Eggins.com
Powershell, .Net, Visual Studio, Team Foundation Server, Windows Communication Foundation

Get-Member is your friend

One of the best cmdlets you can use while learning Powershell is Get-Member. This cmdlet will show all available methods and properties on any given type.

If you execute the following command:
    "Hello" | Get-Member

it will show the following (cut off at the right for brevity):
       TypeName: System.String

    Name             MemberType            Definition
    ----             ----------            ----------
    Clone            Method                System.Object Clone()
    CompareTo        Method                System.Int32 CompareTo(Object value), System.Int3
    Contains         Method                System.Boolean Contains(String value)
    CopyTo           Method                System.Void CopyTo(Int32 sourceIndex, Char[] dest
    EndsWith         Method                System.Boolean EndsWith(String value), System.Boo
    Equals           Method                System.Boolean Equals(Object obj), System.Boolean
    GetEnumerator    Method                System.CharEnumerator GetEnumerator()
    GetHashCode      Method                System.Int32 GetHashCode()
    GetType          Method                System.Type GetType()
    GetTypeCode      Method                System.TypeCode GetTypeCode()
    get_Chars        Method                System.Char get_Chars(Int32 index)
    get_Length       Method                System.Int32 get_Length()
    IndexOf          Method                System.Int32 IndexOf(Char value, Int32 startIndex
    IndexOfAny       Method                System.Int32 IndexOfAny(Char[] anyOf, Int32 start
    Insert           Method                System.String Insert(Int32 startIndex, String val
    IsNormalized     Method                System.Boolean IsNormalized(), System.Boolean IsN
    LastIndexOf      Method                System.Int32 LastIndexOf(Char value, Int32 startI
    LastIndexOfAny   Method                System.Int32 LastIndexOfAny(Char[] anyOf, Int32 s
    Normalize        Method                System.String Normalize(), System.String Normaliz
    PadLeft          Method                System.String PadLeft(Int32 totalWidth), System.S
    PadRight         Method                System.String PadRight(Int32 totalWidth), System.
    Remove           Method                System.String Remove(Int32 startIndex, Int32 coun
    Replace          Method                System.String Replace(Char oldChar, Char newChar)
    Split            Method                System.String[] Split(Params Char[] separator), S
    StartsWith       Method                System.Boolean StartsWith(String value), System.B
    Substring        Method                System.String Substring(Int32 startIndex), System
    ToCharArray      Method                System.Char[] ToCharArray(), System.Char[] ToChar
    ToLower          Method                System.String ToLower(), System.String ToLower(Cu
    ToLowerInvariant Method                System.String ToLowerInvariant()
    ToString         Method                System.String ToString(), System.String ToString(
    ToUpper          Method                System.String ToUpper(), System.String ToUpper(Cu
    ToUpperInvariant Method                System.String ToUpperInvariant()
    Trim             Method                System.String Trim(Params Char[] trimChars), Syst
    TrimEnd          Method                System.String TrimEnd(Params Char[] trimChars)
    TrimStart        Method                System.String TrimStart(Params Char[] trimChars)
    Chars            ParameterizedProperty System.Char Chars(Int32 index) {get;}
    Length           Property              System.Int32 Length {get;}

This shows all of the methods and functions available for the System.String type.

The alias for Get-Member is "gm". I will use the alias throughout the rest of this post. 

You can do this for any type that is loaded into Powershell. For example:
    [System.DateTime] | gm
    [System.DateTime]::now | gm

If you want to know what properties and methods are available for directories and files:
    dir -path c:\windows | gm

You will notice that the above command shows two different types, the DirectloryInfo and FileInfo types.

If you want to know what you can do with the objects returned from a Get-ChildItem performed on a registry node:
    dir -path hklm:\software |gm


Basically, if you have an object, or collection of objects, pipe them into Get-Method, and you will know what it can give you, and what it can do for you.

One last Get-Method Example:
    [System.DateTime] | gm -static

The above command shows all static methods on the [System.DateTime] type. The output is:
       TypeName: System.DateTime

    Name                  MemberType Definition
    ----                  ---------- ----------
    Compare               Method     static System.Int32 Compare(DateTime t1, DateTime t2)
    DaysInMonth           Method     static System.Int32 DaysInMonth(Int32 year, Int32 month)
    Equals                Method     static System.Boolean Equals(DateTime t1, DateTime t2),
    FromBinary            Method     static System.DateTime FromBinary(Int64 dateData)
    FromFileTime          Method     static System.DateTime FromFileTime(Int64 fileTime)
    FromFileTimeUtc       Method     static System.DateTime FromFileTimeUtc(Int64 fileTime)
    FromOADate            Method     static System.DateTime FromOADate(Double d)
    get_Now               Method     static System.DateTime get_Now()
    get_Today             Method     static System.DateTime get_Today()
    get_UtcNow            Method     static System.DateTime get_UtcNow()
    IsLeapYear            Method     static System.Boolean IsLeapYear(Int32 year)
    op_Addition           Method     static System.DateTime op_Addition(DateTime d, TimeSpan
    op_Equality           Method     static System.Boolean op_Equality(DateTime d1, DateTime
    op_GreaterThan        Method     static System.Boolean op_GreaterThan(DateTime t1, DateTi
    op_GreaterThanOrEqual Method     static System.Boolean op_GreaterThanOrEqual(DateTime t1,
    op_Inequality         Method     static System.Boolean op_Inequality(DateTime d1, DateTim
    op_LessThan           Method     static System.Boolean op_LessThan(DateTime t1, DateTime
    op_LessThanOrEqual    Method     static System.Boolean op_LessThanOrEqual(DateTime t1, Da
    op_Subtraction        Method     static System.DateTime op_Subtraction(DateTime d, TimeSp
    Parse                 Method     static System.DateTime Parse(String s), static System.Da
    ParseExact            Method     static System.DateTime ParseExact(String s, String forma
    ReferenceEquals       Method     static System.Boolean ReferenceEquals(Object objA, Objec
    SpecifyKind           Method     static System.DateTime SpecifyKind(DateTime value, DateT
    TryParse              Method     static System.Boolean TryParse(String s, DateTime& resul
    TryParseExact         Method     static System.Boolean TryParseExact(String s, String for
    MaxValue              Property   static System.DateTime MaxValue {get;set;}
    MinValue              Property   static System.DateTime MinValue {get;set;}
    Now                   Property   System.DateTime Now {get;}
    Today                 Property   System.DateTime Today {get;}
    UtcNow                Property   System.DateTime UtcNow {get;}
 


Posted May 28 2007, 11:45 PM by David
Filed under:

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Copyright eggins.com, 1998 - 2008
Powered by Community Server (Non-Commercial Edition), by Telligent Systems