Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Exposing the Mystery of PowerShell Objects

Posted on March 14, 2023March 14, 2023

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.

Get-Member -static

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.

Get-TypeMember help

Specify a type name to discover an object's native members. The command will not show you members added by PowerShell.

Get-TypeMember datetime methods

Static entries will be displayed in green.

get-typemember datetime -name is

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.

Get-TypeMember syntax view

I constructed the Syntax property value as a code snippet you could cut and paste.

Here's where this command is useful.

Math type members
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

9 thoughts on “Exposing the Mystery of PowerShell Objects”

  1. Jeff Stokes says:
    March 15, 2023 at 3:58 am

    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

  2. Pingback: Investigating Powershell Object Members – Curated SQL
  3. Pingback: PowerShell SnippetRace 11/12-2023 | PowerShell Usergroup Austria
  4. Michael Shepard says:
    March 19, 2023 at 3:02 pm

    “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.

    1. Jeffery Hicks says:
      March 20, 2023 at 10:13 am

      You are correct, although piping a class to Get-Member is not intuitive, especially for beginners.

      1. Mike Shepard says:
        March 20, 2023 at 11:06 am

        I agree 100%.

  5. Matt says:
    March 20, 2023 at 11:10 am

    This is not going to train the AI’s very well. we need an exclude (this) from AI Training.

  6. Pingback: PowerShell SnippetRace 13/14-2023 | PowerShell Usergroup Austria
  7. Pingback: Discover Enums with Get-TypeMember • The Lonely Administrator

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d