PowerTip: Find Array Members with Get-Member in PowerShell


Summary: Learn to find members of an array with the Get-Member cmdlet in Windows PowerShell.

Hey, Scripting Guy! Question I have an array of numbers: 1,2,3,4,5 that I assigned to variable $a. When I pipe it to Get-Member: $a | gm,
           all I see are members for int32. How can I find the members of an array by using Get-Member?

Hey, Scripting Guy! Answer There are two ways to do this:

1. Use the uniary operator ( ) in front of the array prior to piping it to Get-Member:

PS C:\> $a = 1,2,3,4,5

PS C:\> ,$a | get-member

2. Use the –InputObject parameter with Get-Member:

Get-Member -InputObject $a

Comments (3)

  1. jrv says:

    Be careful. You could get hurt:L

    PS C:scripts> $a = 1,2,3,4,’x’
    PS C:scripts> $a | get-member

    TypeName: System.Int32

    Name MemberType Definition
    —- ———- ———-
    CompareTo Method int CompareTo(System.Object value), int CompareTo(int value), int IComparable.CompareTo(System.Object obj), int IComparable[int].Comp…

    …..

    TypeName: System.String

    Name MemberType Definition
    —- ———- ———-
    Clone Method System.Object Clone(), System.Object ICloneable.Clone()
    CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB), int IComparable.CompareTo(System.Object obj), int ICo…

    ….

    PS C:scripts>

  2. jrv says:

    Corollary #!

    PS > [int[]]$x=1,2,3,4,5
    PS > $x|gm

    TypeName: System.Int32

    Name MemberType Definition
    —- ———- ———-
    CompareTo Method int CompareTo(System.Object value), int CompareTo(int value), int IComparable.CompareTo(Syste…
    Equals Method bool Equals(System.Object obj), bool Equals(int obj), bool IEquatable[int].Equals(int other)
    GetHashCode Method int GetHashCode()

    PS > [int[]]$x=’1′,’2′,’3′,’4′,’5′
    PS > $x|gm

    TypeName: System.Int32

    Name MemberType Definition
    —- ———- ———-
    CompareTo Method int CompareTo(System.Object value), int CompareTo(int value), int IComparable.CompareTo

  3. For years I’ve been using the comma before an array and never known the name of that operator ("array escaping comma" was my usual name for it). Thanks for clearing up the name for me 🙂

Skip to main content