A few weeks ago, I was working on content for a new PowerShell course for Pluralsight. The subject was objects. We all know the importance of working with objects in PowerShell. Hopefully, you also know that the output you get on your screen from running a PowerShell command is not the whole story. Formatted presentation is separate from the underlying objects in the pipeline. That's why it is important to know how to use Get-Member to discover how an object is defined.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
In my course, I was in a section covering static methods. These are object methods that don't require an instance of the object. In most situations, when you want to invoke an object's method, you need an instance of the object.
$d = Get-Date
$d.AddDays(45)
Get-Date creates a [DateTime] object, which has an AddDays() method. But the [DateTime] class has methods you can invoke that don't require an instance. These are static methods.
PS C:\> [DateTime]::IsLeapYear(2024)
True
Discovering static methods isn't easy. You can use Get-Member, but only if you already have an instance of the object.
But what do you do with a class like [Math]? There's no way to pipe that to Get-Member.
Get-TypeMember
I realized I wanted an alternative to Get-Member, so I wrote Get-TypeMember.
Specify a type name to discover an object's native members. The command will not show you members added by PowerShell.
Static entries will be displayed in green.
The function writes its own type of object to the pipeline, although it is not exposed as a publically available class.
PS C:\> Get-Typemember datetime -Name is* | Select *
Type : System.DateTime
Name : IsDaylightSavingTime
MemberType : Method
PropertyType :
ReturnType : System.Boolean
FieldType :
IsStatic : False
Syntax : $obj.IsDaylightSavingTime()
TypeName : System.DateTime
Type : System.DateTime
Name : IsLeapYear
MemberType : Method
PropertyType :
ReturnType : System.Boolean
FieldType :
IsStatic : True
Syntax : $obj.IsLeapYear([Int32]year)
TypeName : System.DateTime
I included a custom format file with an alternate table view.
I constructed the Syntax property value as a code snippet you could cut and paste.
Here's where this command is useful.
PS C:\> (Get-Typemember math -Name round).Syntax | Select-Object -unique
$obj.Round([Decimal]d)
$obj.Round([Decimal]d,[Int32]decimals)
$obj.Round([Decimal]d,[MidpointRounding]mode)
$obj.Round([Decimal]d,[Int32]decimals,[MidpointRounding]mode)
$obj.Round([Double]a)
$obj.Round([Double]value,[Int32]digits)
$obj.Round([Double]value,[MidpointRounding]mode)
$obj.Round([Double]value,[Int32]digits,[MidpointRounding]mode)
My function is intended to supplement Get-Member, not replace it.
Get It
Want to try it out? Get-TypeMember It is part of the PSScriptTools module, which you can install from the PowerShell Gallery. The function will work in Windows PowerShell and PowerShell 7, including cross-platform.
I hope you'll let me know what you think.
Congratulations on the pluralsight course. FWIW I steer junior admins and people wanting to get into IT towards your “Month of lunches” books 🙂
keep on keepin on sir
Jeff Stokes
“But what do you do with a class like [Math]? There’s no way to pipe that to Get-Member.”
What’s wrong with [math] | Get-Member -Static
You’ve definitely extended things by breaking out the types, though.
You are correct, although piping a class to Get-Member is not intuitive, especially for beginners.
I agree 100%.
This is not going to train the AI’s very well. we need an exclude (this) from AI Training.