Last Update: Sep 04, 2024 | Published: Sep 17, 2015
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.
This limits the output to those properties that were added through PowerShell type extensions. You can also select a single member by name.
Remember that Get-Member writes an object to the pipeline. 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.
A better approach is to also use a hashtable.
Now, you can easily access each member type. Working with method members is especially interesting. 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. 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.
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: 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.
After experimenting with all of this, I decided to go ahead and write a function to easily display an object's methods.
Function Get-Method { [cmdletbinding()] Param( [Parameter(Position=0,Mandatory,ValueFromPipeline)] [ValidateNotNullorEmpty()] [object]$InputObject, [switch]$Static, [ValidateNotNullorEmpty()] [System.Management.Automation.PSMemberViewTypes]$View = "All" ) Begin { Write-Verbose "Starting: $($MyInvocation.Mycommand)" #Add MemberType $PSBoundParameters.Add("MemberType","Method") Write-Verbose "PSBoundParameters $($PSBoundParameters | Out-string)" #initialize an array $in = @() } #begin Process { #add each input object $in+=$InputObject } #process End { If ($Static) { Write-Verbose "Analyzing static methods for object type $($in[0].Fullname)" } else { Write-Verbose "Analyzing methods for object type $($in[0].GetType().Fullname)" } Try { #only need to process one object $PSBoundParameters.InputObject = $in[0] $members = Get-Member @PSBoundParameters -ErrorAction Stop foreach ($method in $members) { $name = $method.name #split overloads #use a regex look behind to split on ), but keep the ) $overloads = $method.definition -split "(?<=))," Foreach ($overload in $overloads) { $overload = $overload.Trim() if ($Static) { #trim and remove 'Static' $overload = $overload.Trim().remove(0,7) } #split into type and syntax $split =$overload.split(" ",2) #create a custom object [pscustomobject]@{ Name = $name OutputType = $split[0] Syntax = $split[1] } } #foreach overload } #foreach } #Try Catch { Throw $_ } #Catch Write-Verbose "Ending: $($MyInvocation.Mycommand)" } #end } #end Get-Method
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:
You can use a cmdlet:
As well as static classes, as long as you remember to use the Static parameter.
I find this especially helpful with WMI classes. 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.