Types and Objects

1. Problem

You have an instance of an object and want to know what methods and properties it supports.

2. Solution

The most common way to explore the methods and properties supported by an object is through the Get-Member cmdlet.

To get the instance members of an object you’ve stored in the $object variable, pipe it to the Get-Member cmdlet:

        $object | Get-MemberGet-Member
        -InputObject $object

To get the static members of an object you’ve stored in the $object variable, supply the –Static flag to the Get-Member cmdlet:

        $object | Get-Member -Static
        Get-Member -Static -InputObject $object

To get the static members of a specific type, pipe that type to the Get-Member cmdlet, and also specify the–Static flag:

        [Type] | Get-Member -Static
        Get-Member -InputObject [Type]

To get members of the specified member type (for example, Method, Property) from an object you have stored in the $object variable, supply that member type to the–MemberType parameter:

        $object | Get-Member -MemberType
        memberTypeGet-Member -MemberType memberType -InputObject $object



3. Discussion

The Get-Member cmdlet is one of the three commands you will use most commonly as you explore Windows PowerShell. The other two commands are Get-Command and Get-Help.

If you pass the Get-Member cmdlet a collection of objects (such as an Array or ArrayList) through the pipeline, PowerShell extracts each item from the collection, and then passes them to the Get-Member cmdlet one-by-one. The Get-Member cmdlet then returns the members of each unique type that it receives. Although helpful the vast majority of the time, this sometimes causes difficulty when you want to learn about the members or properties of the collection class itself.

If you want to see the properties of a collection (as opposed to the elements it contains,) provide the collection to the–InputObject parameter, instead. Alternatively, you may wrap the collection in an array (using PowerShell’s unary comma operator) so that the collection class remains when the Get-Member cmdlet unravels the outer array:

        PS >$files = Get-ChildItem
        PS >,$files | Get-Member


           TypeName: System.Object[]

        Name               MemberType     Definition
        ----               ----------     ----------
        Count              AliasProperty  Count = Length
        Address            Method         System.Object& Address(Int32 )
        (...)
Tags: , , , , , , , , , , , , ,