An object is typed
Each object has a type that can be determined with $obj.GetType()
:
PS C:\Users\Rene> $curDir = get-item .
PS C:\Users\Rene> $curDir.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False DirectoryInfo System.IO.FileSystemInfo
PS C:\Users\Rene> $comObj = new-object -comObject shell.application
PS C:\Users\Rene> $comObj.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False __ComObject System.MarshalByRefObject
PS C:\Users\Rene> $typ = $curDir.GetType()
PS C:\Users\Rene> $typ.GetType().FullName
System.RuntimeType
Properties and methods
The names, member types and definitions of these properties and methods can be seen by
piping an object into
get-member
:
PS C:\Users\Rene> $curDir | get-member
…
In order to restrict the output to properties, the get-member
option -memberType
can be used:
PS C:\Users\Rene> $curDir | get-member -memberType properties
TypeName: System.IO.DirectoryInfo
Name MemberType Definition
---- ---------- ----------
…
FullName Property string FullName {get;}
LastAccessTime Property datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;}
…
In order to see the properties'
values, the object can be piped into
select-object
:
PS C:\Users\Rene> $curDir | select-object -property *
…
FullName : C:\Users\Rene
…
LastAccessTime : 10/12/2019 9:09:47 AM
LastAccessTimeUtc : 10/12/2019 7:09:47 AM
…
The -property
flag is not even required so that the command can be abbreviated with
PS C:\Users\Rene> $curDir | select-object *
PowerShell allows to add properties and methods (collectively referred to as
members) to objects dynamically, at runtime, with the
add-member
cmdLet.
PSStandardMembers
PSStandardMembers
is a hidden
MemberSet. Because it is hidden, it requires the
-force
option of
get-member
to be displayed (as is also the case to display
intrinsic members).
PS C:\> $proc = get-process -id $pid
PS C:\> $proc | get-member -force PSStandardMembers
TypeName: System.Diagnostics.Process
Name MemberType Definition
---- ---------- ----------
PSStandardMembers MemberSet PSStandardMembers {DefaultDisplayPropertySet}
PSStandardMembers
is interesting because it it has a property set with the name DefaultDisplayPropertySet, this property set defines the object's properties that should be displayed when the object is rendered on the console and no view is defined.
PC C:\> $proc.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
Id
Handles
CPU
SI
Name
It turns out, that exactly those properties are displayed in the following (admittedly simple)
pipeline:
PS C:\> $proc | format-list
Id : 2980
Handles : 695
CPU : 4.953125
SI : 1
Name : powershell