Get-member

Get-member and get-help are two of the most used cmdlets when learning (and using PowerShell).  Get-member has some improvements in V2 that are worth exploring. 

I even found some functionality that is in V1 that I wasn’t aware of.  If we start with a simple integer

PS> $x = 27
PS> $x | get-member

   TypeName: System.Int32

Name        MemberType Definition
—-        ———- ———-
CompareTo   Method     System.Int32 CompareTo(Int32 value), System.Int32 CompareTo(Object value)
Equals      Method     System.Boolean Equals(Object obj), System.Boolean Equals(Int32 obj)
GetHashCode Method     System.Int32 GetHashCode()
GetType     Method     System.Type GetType()
GetTypeCode Method     System.TypeCode GetTypeCode()
ToString    Method     System.String ToString(), System.String ToString(IFormatProvider provider), System.String ToS…

This is standard get-member that we know and love.  The bit I’ve just found is -static

PS> $x | get-member -static

   TypeName: System.Int32

Name            MemberType Definition
—-            ———- ———-
Equals          Method     static System.Boolean Equals(Object objA, Object objB)
Parse           Method     static System.Int32 Parse(String s), static System.Int32 Parse(String s, NumberStyles sty…
ReferenceEquals Method     static System.Boolean ReferenceEquals(Object objA, Object objB)
TryParse        Method     static System.Boolean TryParse(String s, Int32& result), static System.Boolean TryParse(S…
MaxValue        Property   static System.Int32 MaxValue {get;}
MinValue        Property   static System.Int32 MinValue {get;}

which shows the static methods available on the object.  This is in V1.

New for V2 is -View which can take the values of  Base, Adapted, Extended  and All.  These restrict the output to particular sets of properties – Base is the original .NET object, Adapted is the properties and methods from the PowerShell extended type system and Extended shows those add via add-member or a types file.  All gets all of them.  This means we don’t need to do

PS> $x.psbase | get-member 

any more.  We do

PS> $x | get-member -View base

instead.  If required this could be into a function as it is often useful to see the base objects especially when dealing with the [ADSI] adapter.

The last addition is -force.  This adds the the psbase, psadapted, psextended, psobject and pstypename members to the list

PS> $x | get-member -force

   TypeName: System.Int32

Name        MemberType   Definition
—-        ———-   ———-
pstypenames CodeProperty System.Collections.ObjectModel.Collection`1[[System.String, mscorlib, Version=2.0.0.0, Cult…
psadapted   MemberSet    psadapted {CompareTo, Equals, GetHashCode, ToString, GetTypeCode, GetType}
psbase      MemberSet    psbase {CompareTo, Equals, GetHashCode, ToString, GetTypeCode, GetType}
psextended  MemberSet    psextended {}
psobject    MemberSet    psobject {Members, Properties, Methods, ImmediateBaseObject, BaseObject, TypeNames, get_Mem…
CompareTo   Method       System.Int32 CompareTo(Int32 value), System.Int32 CompareTo(Object value)
Equals      Method       System.Boolean Equals(Object obj), System.Boolean Equals(Int32 obj)
GetHashCode Method       System.Int32 GetHashCode()
GetType     Method       System.Type GetType()
GetTypeCode Method       System.TypeCode GetTypeCode()
ToString    Method       System.String ToString(), System.String ToString(IFormatProvider provider), System.String T…

These changes are really useful as they allow us to really get under the covers of objects in PowerShell.  When trying to figure out why something isn’t working examining the object(s) with get-member is a very powerful tool.  It just got better.

Technorati Tags: ,

Advertisements
This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s