Tips and Tricks with PowerShell’s Get-Member

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

In “Working with PowerShell’s Get-Member cmdlet,” I guided you through some additional ways to use the Get-Member cmdlet.

Because this cmdlet can expose a lot of information about an object, you might need to fine-tune your expression so that you only see what you need. Although it may not be obvious to do, you can combine parameters to help you in this task.

Combining Get-Member parameters (Image Credit: Jeff Hicks)

Combining Get-Member parameters (Image Credit: Jeff Hicks)

This limits the output to those properties that were added through PowerShell type extensions. You can also select a single member by name.

Selecting a single member (Image Credit: Jeff Hicks)

Selecting a single member (Image Credit: Jeff Hicks)

Remember that Get-Member writes an object to the pipeline.

Getting the members from Get-Member (Image Credit: Jeff Hicks)

Getting the members from Get-Member (Image Credit: Jeff Hicks)

Because Get-Member writes an object to a pipeline, this makes it so that you can manipulate the output from Get-Member. Let’s say you want to group the results.

Grouping members by member type (Image Credit: Jeff Hicks)

Grouping members by member type (Image Credit: Jeff Hicks)

A better approach is to also use a hashtable.

Creating a hashtable of members (Image Credit: Jeff Hicks)

Creating a hashtable of members (Image Credit: Jeff Hicks)

Now, you can easily access each member type.

Referencing individual member types (Image Credit: Jeff Hicks)

Referencing individual member types (Image Credit: Jeff Hicks)

Working with method members is especially interesting.

Enumerating member methods (Image Credit: Jeff Hicks)

Enumerating member methods (Image Credit: Jeff Hicks)

Sponsored

Looking at this output, it might be handy to break this down further so that I can see the method name, its output type, and syntax. The information is all there, I just to break it a part from the definition. But I’m going to do more than simply parse text; I want to write a new object to the pipeline with the information I want. This doesn’t require a hashtable, so let me start over.

Now, I have a collection of method members for process objects.

Process method members (Image Credit: Jeff Hicks)

Process method members (Image Credit: Jeff Hicks)

The Name is easy to get, but I’ll have to break down the definition. The first part should indicate the method’s output type, such as String or Boolean. Next is the syntax. But some methods are overloaded, meaning there are multiple syntax options. These are separated by commas. I could split the syntax portion on a comma, but that would mess things up for methods that take multiple parameters, which will also be separated by a comma. My solution is to use a regular expression the looks behind a comma for a closing parenthese. If it finds one, I’ll split there. Here’s the code snippet I came up with.

Extracted object methods (Image Credit: Jeff Hicks)

Extracted object methods (Image Credit: Jeff Hicks)

I don’t know about you, but I find this much easier to read. The same code doesn’t quite work for static methods.

Using the same code above, gives me this:

Listing static methods (Image Credit: Jeff Hicks)

Listing static methods (Image Credit: Jeff Hicks)

In this case, the output type is part of the definition. My solution was to remove the ‘static’ keyword from the beginning of the definition and then split what was left to get the type and syntax.

Revised static member listing (Image Credit: Jeff Hicks)

Revised static member listing (Image Credit: Jeff Hicks)

After experimenting with all of this, I decided to go ahead and write a function to easily display an object’s methods.

The function is essentially a wrapper for Get-Member, so it uses the same parameters. This makes it easy to splat the bound parameters (i.e. $PSBoundParameters) to Get-Member. This function works for single objects:

Listing methods for a single object (Image Credit: Jeff Hicks)

Listing methods for a single object (Image Credit: Jeff Hicks)

You can use a cmdlet:

Extracting methods from cmdlet output (Image Credit: Jeff Hicks)

Extracting methods from cmdlet output (Image Credit: Jeff Hicks)

As well as static classes, as long as you remember to use the Static parameter.

Convert static methods (Image Credit: Jeff Hicks)

Convert static methods (Image Credit: Jeff Hicks)

I find this especially helpful with WMI classes.

Listing WMI class methods (Image Credit: Jeff Hicks)

Listing WMI class methods (Image Credit: Jeff Hicks)

Sponsored

All of this came about because I took the time to see what else I could do with Get-Member. Of course, I hope you will play around with Get-Member and other PowerShell cmdlets and let me know what you find.

Sponsored

Tagged with ,