Working with PowerShell’s Get-Member cmdlet
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.
- Related: How to Learn PowerShell
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.
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.
There are several different member types that you can see.
You can also display a comma-separated list. 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. See how the typename is System.Runtime and not System.Math? Let's try this instead:
You can use the :: operator to invoke a static method. 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. 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. 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. 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.
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. 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.
More in PowerShell
How to Use a PowerShell Foreach Loop
Oct 24, 2022 | Ivan Mirchev
How to Use a PowerShell Array
Oct 19, 2022 | Michael Reinders
Filtering with PowerShell Where-Object: Easy Examples
Oct 10, 2022 | Michael Reinders
What is PowerShell and How to Get Started With It?
Sep 7, 2022 | Mike Kanakos
Use a PowerShell Substring to Search Inside a String
Aug 11, 2022 | Jeff Hicks
Test Network Connectivity with PowerShell Test-Connection
Aug 9, 2022 | Jeff Hicks
Most popular on petri