PowerShell Pipeline

Exploring PowerShell, Part 3: Using Get-Member To Dig into .NET Objects

Rounding out my series on exploring PowerShell is our last stop at checking out Get-Member, which is one of the cmdlets that you should definitely know.

More on This Topic:

Where Get-Command and Get-Help showed you how to find a command and learn more about how to use it, Get-Member provides the ability to dive into the objects that are the result of your commands and allows you to better learn about what is available to use. There is a wealth of information just hiding in the object that can aid you in running some method that doesn't have a cmdlet available or understanding how some properties are not native to the .NET objet itself and are added through a specific type file.

Whenever we use a cmdlet, such as Get-Process or Get-ChildItem, an object will be returned that you can use to write to a file or perform some other type of operation such as sorting or filtering. But what is actually in the object and how can we look at it under the hood to see what else might be lying around? The answer is to use Get-Member against the object and take a look at all of the member types.

The first thing we need to do is get an object to inspect which we can do by using Get-ChildItem and only pull back directories.

$Directory = Get-ChildItem -Directory 

Now that we have our object, we can start checking it out using Get-Member to see what is all within it.

$Directory | Get-Member 
[Click on image for larger view.]  Figure 1. The Member Types of System.IO.DirectoryInfo object.

What we can see are the various member types such as properties and methods. Sometimes, if an object has it, we will event see Event member types as well that we could use with a Register-ObjectEvent cmdlet to perform an action.

We can easily filter for various things when using Get-Member, such as just looking for Methods in the object.

$Directory | Get-Member -MemberType Method

Perhaps we want to change the view that is given to us when calling the cmdlet to show all of the members which have been added to the object through the use of a types.ps1xml file or done via the Add-Member cmdlets.

$Directory | Get-Member -View Extended 
[Click on image for larger view.]  Figure 2. Viewing properties that have been added to the .Net object.

If you want to see everything, including intrinsic members that are not shown in the default view, you can use the –Force parameter to get a look at all of the other members.

$Directory | Get-Member -Force 
[Click on image for larger view.]  Figure 3. Viewing all of the members of the .Net object using -Force.

Another fun example can be done involving Windows Management Instrumentation (WMI). If you have ever worked with WMI, you know that there are some very useful methods that are in specific classes. I'll use the Win32_Service class as an example.

Get-WMIObject -Class Win32_Service |
Get-Member -MemberType Method
[Click on image for larger view.]  Figure 4.

What is interesting in not only viewing the available methods (many of which are pretty useful such as being able to modify various parts of a service), we can actually see that two different object types are returned based on the kind of service. In this case, the majority of the services are in fact Win32_Service objects but there are at least one that is a Win32_TerminalService class.

Now let's take a look at what happens when we attempt to view the members of a collection object by piping the object into Get-Member. But, first, I had better create my collection first.

[System.Collections.ArrayList]$List  = 1,5,3243323432443,'moretest','test'
$List | Get-Member

The result is larger than what I would like to show you with a picture, but basically what you end up seeing are the .Net objects for each type that was added to the collection ([string],[int32] and [int64]). So why was the actual System.Collections.ArrayList not shown instead? The answer is that PowerShell unrolls the collection and passes each item through the pipeline into Get-Member, thus it only sees each item as its own type and then displays that relevant information back to you.

If you are looking to view the actual collection object instead, you must use the –InputObject parameter in Get-Member and supply the collection to that.

Get-Member -input $List 
[Click on image for larger view.]  Figure 5. Viewing the collection object.

Here we can actually see the collection object and what the possible methods and properties are. No automatic unrolling of the data happening now that we have shifted from piping this to a cmdlet to using a parameter name instead.

So far I have been showing you how you can use Get-Member to view objects that are returned when running various commands, but that isn't all that we can use this for! We can take a look at static methods which are available in certain .Net types that you have access to in PowerShell, such as [System.Math].

[math] | Get-Member -Static
[Click on image for larger view.]  Figure 6. Static methods for System.Math.

This is some great stuff! We can easily look at the static methods and choose one to use if we need it. Let's just try out one of these static methods:\

[Math]::PI 
[Click on image for larger view.]  Figure 7. Look at the first14 digits of Pi using a static method.

So you might be thinking that we can throw out the –Static parameter and view all of the methods and properties of System.Math and it will just work out that way.

[math] | Get-Member 
[Click on image for larger view.]  Figure 8. Some results when using Get-Member on a type without using -Static.

The problem is that the PowerShell type is a System.RunType and therefore, will return the appropriate methods and properties that are resident in that specific type. Definitely something to keep in mind when you are starting to delve deeper into .Net through PowerShell.

With that, you should now have some more info in your repertoire to begin looking at the objects being returned and can make use of them when you see fit. This also shows just how great PowerShell is in allowing ways to explore the objects to get a better understand of what they are and how you can utilize them in your day-to-day coding.

 

About the Author

Boe Prox is a Microsoft MVP in Windows PowerShell and a Senior Windows System Administrator. He has worked in the IT field since 2003, and he supports a variety of different platforms. He is a contributing author in PowerShell Deep Dives with chapters about WSUS and TCP communication. He is a moderator on the Hey, Scripting Guy! forum, and he has been a judge for the Scripting Games. He has presented talks on the topics of WSUS and PowerShell as well as runspaces to PowerShell user groups. He is an Honorary Scripting Guy, and he has submitted a number of posts as a to Microsoft's Hey, Scripting Guy! He also has a number of open source projects available on Codeplex and GitHub. His personal blog is at http://learn-powershell.net.

comments powered by Disqus
Most   Popular