Getting Started with PowerShell's Get-Member
If you are going to get anywhere with PowerShell you have to absolutely understand the concept of objects in the pipeline. In an earlier article I attempted to explain objects and how they work in PowerShell. In that article I mentioned the Get-Member cmdlet. This is a key command you need to be familiar with, so let’s spend a few minutes getting acquainted.
You can pipe any command or object to Get-Member, which has an alias of gm. If the piped objects are of different types, you will get a result from Get-Member for each type. But here’s a simple example.
The first thing to notice is the TypeName. Anything that starts with System is part of the .NET Framework, not that it should make much difference. But in PowerShell you can have commands that reference a .NET class but drop ‘System’. The only time you might need to know the typename is if you need to search online for some background information. Most likely you’ll end up on an MSDN page for the given class. You should see descriptions of what you see in Get-Member. However, be aware that PowerShell might add members, and you can add members as well.
Read the Best Personal and Business Tech without Ads
Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.
In the output, you can see Properties and Methods, which is all you need to be concerned about. I don’t even bother with Event members. The definition for each property member will indicate what type of object it is and whether it is read-only or not.
The property name is important to know and recognize. The default display you might see with a command could be very different than the actual property names. But you can use any property name you see in the list with object-related cmdlets like Select-Object or Where-Object.
You don’t need to concern yourself with most of the methods because, hopefully, there is a corresponding cmdlet you can use. And some of the methods are related to .NET programming that you won’t be doing. But if you find an exception where you need to invoke a method directly, Get-Member provides information you might need.
The method might write some sort of object to the pipeline like a string or Boolean. Or it might not write anything indicated by void. You can also see if there are any method parameters. Sometimes it is hard to see in the default display. But you can always use Get-Member to get a specific member.
Sometimes you can tell how to use it from the description. Other times you might need to turn to MSDN and do a little research.
If you spend any time piping things to Get-Member you’ll see that some objects have a variety of other member types. Process objects are a great example.
An alias property is an alternate name intended to be more meaningful than the programmatic name, or easier to type. You can ask Get-Member to only display any of these types.
A NoteProperty is usually some sort of static value that is programmatically assigned. They may or may not be useful. A ScriptProperty is something added by PowerShell. The value is calculated on the fly through a predefined PowerShell scriptblock. These are properties not part of the original object but something someone thought would be useful. You might also see a ScriptMethod, which is an additional method implemented through PowerShell script.
You can use these properties like any other property.
Lastly, a PropertySet is a predefined collection of properties. Again, these were added by someone who thought this group of information would be useful.
So instead of running Get-Process and then selecting the Name,ID,PriorityClass and FileVersion properties, you can use the Property set.
Once you know the different types of members, you can use Get-Member to limit its results as I did earlier. Or you can use commands like these:
Get-service | Get-Member -MemberType Properties Get-service | Get-Member -MemberType Methods
Don’t forget that Get-Member is just another cmdlet, which means it too writes objects to the pipeline.
There’s nothing preventing you from using the PowerShell pipeline to display the information just the way you want it.
I should point out that there is a bit more to Get-Member than what I’m covering here. But those features are aimed at more experienced users or developers. By default PowerShell tries to shield some of the sausage-making parts from you. If you truly want to see everything, you can pipe to Get-Member and use the -Force parameter. This should show you all of the hidden members, as well. Try this out, and you’ll understand why they are hidden!
There is a great deal of hidden treasure that you can uncover with Get-Member. Don’t limit yourself to what you see by default. Dig in. Explore and see where Get-Member takes you. If you go somewhere unexpected I hope you’ll share your experiences in the comments.