Using PowerShell Get-Member to Explore the .NET Framework

Using PowerShell Get-Member to Explore the .NET Framework

  • Comments 2
  • Likes

 

 

Summary: Learn how to use the Windows PowerShell Get-Member cmdlet to explore the .NET Framework in this interactive how-to post.

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! When you give examples of using the Get-Member cmdlet to explore the members of something in Windows PowerShell it seems so easy. The problem is that when I use it, things do not seem so easy. Sometimes it works, and other times it provides totally bogus information. Can you help me discover what I am doing wrong?

-- PN

 

Hey, Scripting Guy! AnswerHello PN, Microsoft Scripting Guy Ed Wilson here. It is early in the morning, and there is fog hanging close to the ground. Last night the wind howled, the leaves rustled, and the rain came down like a heavy mist for most of the night. At times, the moon was peeking from behind the high clouds. It was a cool night, figuratively and literally. I am sipping on a cup of English Breakfast tea with a cinnamon stick in it, and munching on a freshly baked slice of pumpkin bread. I have my Zune HD cranked up and it is blasting some classic James Brown. It is a retro morning as I weed my way through some of the questions sent to Scripter@Microsoft.Com.

Speaking of retro classics, PN your question reminds me of the things that bugged me when I was just learning Windows PowerShell, and that is the seemingly inconsistent behavior of the Get-Member Windows PowerShell cmdlet. If I assign an integer to a variable, and then pipeline that variable to the Get-Member cmdlet it correctly detects the item as an instance of the System.Int32 .NET Framework class (int32 is the class, System is the namespace) and displays the members (methods, properties, events). The Int32 class does not expose any properties or events. (One thing that this shows is that by default Windows PowerShell treats an integer as an int32). This is seen here.

PS C:\> $int = 5
PS C:\> $int | Get-Member


   TypeName: System.Int32

Name        MemberType Definition
----        ---------- ----------
CompareTo   Method     int CompareTo(System.Object value), int CompareTo(int value)
Equals      Method     bool Equals(System.Object obj), bool Equals(int obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
GetTypeCode Method     System.TypeCode GetTypeCode()
ToString    Method     string ToString(), string ToString(string format), string ...


PS C:\>

 

If I want to view the static members I use the Get-Member Windows PowerShell cmdlet with the -static switch as seen here.

PS C:\> $int | Get-Member -Static

 

 

   TypeName: System.Int32

 

Name            MemberType Definition

----            ---------- ----------

Equals          Method     static bool Equals(System.Object objA, System.Object o...

Parse           Method     static int Parse(string s), static int Parse(string s,...

ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System...

TryParse        Method     static bool TryParse(string s, System.Int32&, mscorlib...

MaxValue        Property   static System.Int32 MaxValue {get;}

MinValue        Property   static System.Int32 MinValue {get;}

 

 

PS C:\>

 

But what happens when I store an array of integers in a variable named $array? As seen here, the Get-Member Windows PowerShell cmdlet reports that I have an Int32.

PS C:\> $array = 1,2,3,4
PS C:\> $array | Get-Member


   TypeName: System.Int32

Name        MemberType Definition
----        ---------- ----------
CompareTo   Method     int CompareTo(System.Object value), int CompareTo(int value)
Equals      Method     bool Equals(System.Object obj), bool Equals(int obj)
GetHashCode Method     int GetHashCode()
GetType     Method     type GetType()
GetTypeCode Method     System.TypeCode GetTypeCode()
ToString    Method     string ToString(), string ToString(string format), string ...


PS C:\>

 

If I have an array that contains a mixture of integers and strings, Get-Member will report that it contains both types. This is shown here.

PS C:\> $mixArray = 1,2,"string",3

PS C:\> $mixArray | Get-Member

 

 

   TypeName: System.Int32

 

Name        MemberType Definition

----        ---------- ----------

CompareTo   Method     int CompareTo(System.Object value), int CompareTo(int value)

Equals      Method     bool Equals(System.Object obj), bool Equals(int obj)

GetHashCode Method     int GetHashCode()

GetType     Method     type GetType()

GetTypeCode Method     System.TypeCode GetTypeCode()

ToString    Method     string ToString(), string ToString(string format), string ...

 

 

   TypeName: System.String

 

Name             MemberType            Definition

----             ----------            ----------

Clone            Method                System.Object Clone()

CompareTo        Method                int CompareTo(System.Object value), int Co...

Contains         Method                bool Contains(string value)

CopyTo           Method                System.Void CopyTo(int sourceIndex, char[]...

EndsWith         Method                bool EndsWith(string value), bool EndsWith...

Equals           Method                bool Equals(System.Object obj), bool Equal...

GetEnumerator    Method                System.CharEnumerator GetEnumerator()

GetHashCode      Method                int GetHashCode()

GetType          Method                type GetType()

GetTypeCode      Method                System.TypeCode GetTypeCode()

IndexOf          Method                int IndexOf(char value), int IndexOf(char ...

IndexOfAny       Method                int IndexOfAny(char[] anyOf), int IndexOfA...

Insert           Method                string Insert(int startIndex, string value)

IsNormalized     Method                bool IsNormalized(), bool IsNormalized(Sys...

LastIndexOf      Method                int LastIndexOf(char value), int LastIndex...

LastIndexOfAny   Method                int LastIndexOfAny(char[] anyOf), int Last...

Normalize        Method                string Normalize(), string Normalize(Syste...

PadLeft          Method                string PadLeft(int totalWidth), string Pad...

PadRight         Method                string PadRight(int totalWidth), string Pa...

Remove           Method                string Remove(int startIndex, int count), ...

Replace          Method                string Replace(char oldChar, char newChar)...

Split            Method                string[] Split(Params char[] separator), s...

StartsWith       Method                bool StartsWith(string value), bool Starts...

Substring        Method                string Substring(int startIndex), string S...

ToCharArray      Method                char[] ToCharArray(), char[] ToCharArray(i...

ToLower          Method                string ToLower(), string ToLower(System.Gl...

ToLowerInvariant Method                string ToLowerInvariant()

ToString         Method                string ToString(), string ToString(System....

ToUpper          Method                string ToUpper(), string ToUpper(System.Gl...

ToUpperInvariant Method                string ToUpperInvariant()

Trim             Method                string Trim(Params char[] trimChars), stri...

TrimEnd          Method                string TrimEnd(Params char[] trimChars)

TrimStart        Method                string TrimStart(Params char[] trimChars)

Chars            ParameterizedProperty char Chars(int index) {get;}

Length           Property              System.Int32 Length {get;}

 

 

PS C:\>

 

All this information is well and good, but I thought I had an array. Where is that information? There are, as you may know, two ways of using the Get-Member cmdlet. The first way, the way I use it most frequently, involves piping an object to the cmdlet. The second way is to pass the object to the inputobject parameter. This technique is illustrated here with the associated output.

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

PS C:\> Get-Member -InputObject $array

 

 

   TypeName: System.Object[]

 

Name           MemberType    Definition

----           ----------    ----------

Count          AliasProperty Count = Length

Address        Method        System.Object&, mscorlib, Version=2.0.0.0, Culture=n...

Clone          Method        System.Object Clone()

CopyTo         Method        System.Void CopyTo(array array, int index), System.V...

Equals         Method        bool Equals(System.Object obj)

Get            Method        System.Object Get(int )

GetEnumerator  Method        System.Collections.IEnumerator GetEnumerator()

GetHashCode    Method        int GetHashCode()

GetLength      Method        int GetLength(int dimension)

GetLongLength  Method        long GetLongLength(int dimension)

GetLowerBound  Method        int GetLowerBound(int dimension)

GetType        Method        type GetType()

GetUpperBound  Method        int GetUpperBound(int dimension)

GetValue       Method        System.Object GetValue(Params int[] indices), System...

Initialize     Method        System.Void Initialize()

Set            Method        System.Void Set(int , System.Object )

SetValue       Method        System.Void SetValue(System.Object value, int index)...

ToString       Method        string ToString()

IsFixedSize    Property      System.Boolean IsFixedSize {get;}

IsReadOnly     Property      System.Boolean IsReadOnly {get;}

IsSynchronized Property      System.Boolean IsSynchronized {get;}

Length         Property      System.Int32 Length {get;}

LongLength     Property      System.Int64 LongLength {get;}

Rank           Property      System.Int32 Rank {get;}

SyncRoot       Property      System.Object SyncRoot {get;}

 

 

PS C:\>

 

At first brush, it seems that the command has not worked as intended. After all, it says that it is a System.Object[]. But the square brackets tell me that I have an array, and if I compare the results with the members detailed for the System.Array .NET Framework class on MSDN shown in the following figure, it shows that I am in fact, working with an instance of a System.Array.

 

All the properties are surfaced through the Get-Member cmdlet. One thing that seems to be amiss is that the methods seem to be missing. The key to understanding the results that I have obtained is in realizing that I am working with an instance of the System.Array class. Instance members (properties and methods) are available as soon as an instance of the class has been created. Static members are available without having to create an instance of the class. As discussed yesterday, static members in MSDN are indicated with a big red "S".

The static members of the System.Array .NET Framework class are seen here.

PS C:\> [system.array] | Get-Member -Static


   TypeName: System.Array

Name            MemberType Definition
----            ---------- ----------
AsReadOnly      Method     static System.Collections.ObjectModel.ReadOnlyCollecti...
BinarySearch    Method     static int BinarySearch(array array, System.Object val...
Clear           Method     static System.Void Clear(array array, int index, int l...
ConstrainedCopy Method     static System.Void ConstrainedCopy(array sourceArray, ...
ConvertAll      Method     static TOutput[] ConvertAll[TInput, TOutput](TInput[] ...
Copy            Method     static System.Void Copy(array sourceArray, array desti...
CreateInstance  Method     static array CreateInstance(type elementType, int leng...
Equals          Method     static bool Equals(System.Object objA, System.Object o...
Exists          Method     static bool Exists[T](T[] array, System.Predicate[T] m...
Find            Method     static T Find[T](T[] array, System.Predicate[T] match)
FindAll         Method     static T[] FindAll[T](T[] array, System.Predicate[T] m...
FindIndex       Method     static int FindIndex[T](T[] array, System.Predicate[T]...
FindLast        Method     static T FindLast[T](T[] array, System.Predicate[T] ma...
FindLastIndex   Method     static int FindLastIndex[T](T[] array, System.Predicat...
ForEach         Method     static System.Void ForEach[T](T[] array, System.Action...
IndexOf         Method     static int IndexOf(array array, System.Object value), ...
LastIndexOf     Method     static int LastIndexOf(array array, System.Object valu...
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System...
Resize          Method     static System.Void Resize[T](T[]& array, int newSize)
Reverse         Method     static System.Void Reverse(array array), static System...
Sort            Method     static System.Void Sort(array array), static System.Vo...
TrueForAll      Method     static bool TrueForAll[T](T[] array, System.Predicate[...


PS C:\>

 

The methods on MSDN that are not marked with a big red "S" are shown largely in the display of the System.Object[] output.

PN, that is all there is to using Get-Member to discover hidden members. I encourage you to play around with the output, and compare results with the class descriptions on MSDN. .NET Framework week will continue tomorrow when I will talk about additional ways to explore classes using the Get-Member cmdlet.

I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>cranked up and it is &quot;blasting some classic James Brown&quot;</p> <p>Now your talking, :) a man after my own heart</p> <p>Ernie</p>

  • <p>Powershell is the worst language (scripting or programming) I have ever had the misfortune of working with.</p>