CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Get-Member cmdlet – Gets the properties and methods of objects

PowerShell is developed using .Net framework. The commands using in PowerShell are internally access .Net framework classes, it’s methods and properties etc, to show the related results.

The results shown for each PowerShell cmdlet is the collection of objects. Each of the object has it’s members; the members are methods, properties etc,. We can use these members to filter or fine tune the results from the commands.

How do we know object’s members? PowerShell provides Get-Member cmdlet to know the list of members of the objects.

The Get-Member cmdlet

Get-Member cmdlet is used to get the members of the objects; the methods, properties, events etc, associated with the objects. It always need an input to get the members. If you call this command without passing any input, it will throw the below Error.

PS C:\> Get-Member
Get-Member : You must specify an object for the Get-Member cmdlet.
At line:1 char:1
+ Get-Member
+ ~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException
+ FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

We can pass inputs to Get-Member cmdlet, through pipeline or through it’s optional parameter -InputObject. But it will behave differently when you use the -InputObject parameter to pass the input. Let’s look how this will work with both pipeline and passing input through it’s parameter.

PS C:\> Get-ChildItem | Get-Member

Above command displays all the members associated with each object. Get-ChildItem returns the list of files and folders in the current directory. And this will be the input to Get-Member cmdlet. It takes each object from the collection, and displays the members of it.

You will see the members of System.IO.DirectoryInfo and System.IO.FileInfo as Get-ChildItem returns both files and directories.

We will see how this command behaves when we pass the input through it’s parameter -InputObject.

PS C:\> Get-Member -InputObject Get-ChildItem

Observe that, the results were completely mismatch with the above results when we pass the input through pipeline. When you give the input through the parameter, it will NOT go through each object in the collection; instead it considers whole collection as an object and displays the members of the collection object.

If you pass a single object as an input through this parameter, it returns the members of it’s object.

PS C:\> Get-Member -InputObject 1

Now you will see different results; the members of Int32 object.

We will discuss more topics as we go.

[..] David

PowerShell – Get-Member cmdlet – Gets the properties and methods of objects

2 thoughts on “PowerShell – Get-Member cmdlet – Gets the properties and methods of objects

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top