Get-Member (Alias: gm)
Last updated
Last updated
When working with PowerShell, it is crucial to understand the properties and methods of objects you are dealing with. One of the most useful cmdlets for this purpose is Get-Member
. This cmdlet provides insights into the structure of objects, allowing users to interact effectively with them.
Get-Member
, and Why is it Important?Get-Member
is a cmdlet in PowerShell that reveals the properties, methods, and events available on an object. Each object in PowerShell is based on a .NET class, and Get-Member
allows users to inspect these objects to understand their capabilities.
Get-Member
?When working with unfamiliar objects, Get-Member
helps identify the available properties and methods.
It enables users to discover how they can manipulate or retrieve data from an object.
It is useful when troubleshooting scripts, as it provides a clear view of what an object contains.
This command retrieves a list of running processes and pipes them into Get-Member
. The output displays:
The type/class name (System.Diagnostics.Process
)
The list of properties and methods available for process objects.
Get-Member
WorksWhenever an object is piped into Get-Member
, it inspects the first object in the pipeline and returns:
The type or class name of the object.
The properties and methods available for that object.
Get-Item HKLM:\
retrieves an item from the registry.
Piping $x
into Get-Member
displays its available members.
This is useful when dealing with objects of unknown types.
Get-Member
with Different Object TypesWhen working with collections (arrays, lists, etc.), it is important to differentiate between inspecting an individual item and inspecting the collection itself.
Example 1: Inspecting the First Item in a Collection
This command retrieves items from C:\
(files and directories).
Piping $output
into Get-Member
shows the members of the first item in the collection.
Example 2: Inspecting the Collection Itself
Using -InputObject $output
inspects the array itself (System.Object[]
), revealing members like Count
, Length
, and SyncRoot
.
Get-Member
Handles Mixed Object TypesIf multiple object types are piped into Get-Member
, it does not list the members of every single object instance. Instead, it groups objects by type and displays the members for each unique type.
Example: Mixed Object Types
If Get-ChildItem
returns a mix of files and directories, Get-Member
will display members separately for System.IO.FileInfo
and System.IO.DirectoryInfo
.
Get-Member
-MemberType
ParameterTo filter results, use the -MemberType
parameter.
This command displays only the properties of System.Diagnostics.Process
.
This command displays only the methods of System.Diagnostics.Process
.
-Name
Parameter to Search for a Specific MemberThis checks if HandleCount
exists as a property or method.
This displays static methods and properties of System.Math
.