Working with PowerShell’s Get-Member cmdlet

Posted on September 10, 2015 by Jeff Hicks in PowerShell with 0 Comments

One of the first cmdlets that you will learn when diving into PowerShell is the Get-Member cmdlet. This is a very handy cmdlet because it helps you discover what you can do with different object types. All you have to do is pipe any command or object to Get-Member, and it will tell you everything that can be seen about the underlying object.

Using PowerShell's Get-Member cmdlet (Image Credit: Jeff Hicks)

Using PowerShell’s Get-Member cmdlet (Image Credit: Jeff Hicks)

PowerShell’s Get-Member cmdlet is one of those commands that you see demonstrated and you think that’s all there is to it. With that sia,d there’s more to this cmdlet than most people realize, so let’s dig in.

If you look at help for Get-Member, then you might be surprised that there are a few other parameters.

Get-Member help (Image Credit: Jeff Hicks)

Get-Member help (Image Credit: Jeff Hicks)

This is why I always read help, even for commands you think you know.

One way you can use Get-Member is to limit what you get back. Only interested in an object’s properties? Then ask to see only properties.

Viewing service properties only (Image Credit: Jeff Hicks)

Viewing service properties only (Image Credit: Jeff Hicks)

There are several different member types that you can see.

A list of member types (Image Credit: Jeff Hicks)

A list of member types (Image Credit: Jeff Hicks)

You can also display a comma-separated list.

Getting multiple member types (Image Credit: Jeff Hicks)

Getting multiple member types (Image Credit: Jeff Hicks)

Without getting into .NET too deeply, you should understand that some object classes have static members. You generally see these types of members with classes like [Math]. Because of the way that PowerShell tries to make your life easier, if you try to look at [Math] with Get-Member, it doesn’t appear very useful.

Examining the [Math] class with Get-Member (Image Credit: Jeff Hicks)

Examining the [Math] class with Get-Member (Image Credit: Jeff Hicks)

See how the typename is System.Runtime and not System.Math? Let’s try this instead:

Viewing static methods of the [Math] class (Image Credit: Jeff Hicks)

Viewing static methods of the [Math] class (Image Credit: Jeff Hicks)

You can use the :: operator to invoke a static method.

Invoking a static member (Image Credit: Jeff Hicks)

Invoking a static member (Image Credit: Jeff Hicks)

The other thing to know about Get-Member is that by default it only tells you what it thinks you want to know or are capable of knowing. As I mentioned a moment ago, PowerShell does a lot of work on your behalf to transform native .NET classes into things that are easy for you to use. Although you don’t have to be a .NET developer to use PowerShell effectively, it helps if you understand where everything comes from.

In many cases, PowerShell has rules that take a native .NET class like System.ServiceProcess.ServiceController and extend it by adding members. You can see different views of an object with the –View parameter. The base view should be the original or underlying class.

The Base view of a service object (Image Credit: Jeff Hicks)

The Base view of a service object (Image Credit: Jeff Hicks)

There’s also an adapted view that includes members added by PowerShell’s extended type system. Depending on the object class, there may be no difference between the base and adapted views. Finally there is the extended view, which should show you members added by PowerShell via the Types.ps1xml file or the Add-Member cmdlet.

The Extended view of a service object (Image Credit: Jeff Hicks)

The Extended view of a service object (Image Credit: Jeff Hicks)

These are few items that PowerShell added. If you have added your own extensions, these will also be displayed. I have extended the file object.

My extensions to the file object (Image Credit: Jeff Hicks)

My extensions to the file object (Image Credit: Jeff Hicks)

The default view is Adapted and Extended. You can also specify that all views are set to default. And if you really want to get down to the nuts and bolts, you can use the –Force parameter.

Forcing a display of all members (Image Credit: Jeff Hicks)

Forcing a display of all members (Image Credit: Jeff Hicks)

On my computer, this gives me 57 members. I encourage you to give this command a try on your desktop to see for yourself. I have to point out that even though you may see a lot of members, it is unlikely that you will ever have a need to use them in a command or script. There should be PowerShell cmdlet or parameter that will take care of things for you. Much of what you see is .NET details that you don’t really need to know, which is why it is kept out of view by default.

Sponsored

Get-Member should be one of your favorite cmdlets to use, and I hope you’ll take some time to explore what it can show you. In another article, I’ll share some additional tips and tricks with Get-Member.

Sponsored

Tagged with