Using the Get-Member Cmdlet


The Get-Member cmdlet retrieves information about the members of objects. Although this may not seem very exciting, remember that because everything returned from a cmdlet is an object, we can use the Get-Member cmdlet to examine the methods and properties of objects. When the Get-Member cmdlet is used with Get-ChildItem on the filesystem, it returns a listing of all the methods and properties available to work with the filesystem object.

image from book
Objects, Properties, and Methods

One of the more interesting features of Windows PowerShell is that cmdlets return objects. An object is a thing that gives us the ability to either describe something or do something. If we are not going to describe or do something, then there is no reason to create the object. Depending on the circumstances, we may be more interested in the methods, or the properties. As an example, let’s consider rental cars. I travel a great deal in my role as a consultant at Microsoft, and I often need to obtain a rental car.

When I get to the airport, I go to the rental car counter, and I use the New-Object cmdlet to create the rentalCAR object. When I use this cmdlet, I am only interest in the methods available from the rentalCAR object. I will need to use the DriveDowntheRoad method, the StopAtaRedLight method, and perhaps the PlayNiceMusic method. I am not, however, interested in the properties of the rentalCAR object.

At home, I have a cute little sports car. It has exactly the same methods as the rentalCAR object, but I created the sportsCAR object primarily because of its properties. It is green and has alloy rims, a convertible top, and a 3.5-liter engine. Interestingly enough, it has exactly the same methods as the rentalCAR object. It also has the DriveDowntheRoad method, the StopAtaRedLight method, and the PlayNiceMusic method, but the deciding factor in creating the sportsCAR object was the properties, not the methods.

image from book

Just the Steps 

Using the Get-Member cmdlet to examine properties and methods In a Windows PowerShell prompt, enter the Get-ChildItem cmdlet followed by the path to a folder and pipe it to the Get-Member cmdlet. Example:

 get-childitem C:\ | get-member

  1. Start Windows PowerShell by using Start | Run | Windows PowerShell. The PowerShell prompt will open by default at the root of your Documents And Settings.

  2. Use an alias to refer to the Get-Alias cmdlet. To find the correct alias, use the Get-Alias cmdlet as follows:

     get-alias g*

  3. After you have retrieved the alias for the Get-Alias cmdlet, use it to find the alias for the Get-Member cmdlet. One way to do this is to use the following command, simply using gal in place of the Get-Alias name you used in the previous command:

     gal g*

  4. The listing of aliases defined that begin with the letter g appears as a result of the previous command. The output is shown here:

     CommandType     Name                            Definition -----------     ----                            ---------- Alias           gal                             Get-Alias Alias           gc                              Get-Content Alias           gci                             Get-ChildItem Alias           gcm                             Get-Command Alias           gdr                             Get-PSDrive Alias           ghy                             Get-History Alias           gi                              Get-Item Alias           gl                              Get-Location Alias           gm                              Get-Member Alias           gp                              Get-ItemProperty Alias           gps                             Get-Process Alias           group                           Group-Object Alias           gsv                             Get-Service Alias           gsnp                            Get-PSSnapin Alias           gu                              Get-Unique Alias           gv                              Get-Variable Alias           gwmi                            Get-WmiObject Alias           gh                              Get-Help

  5. Use the gal alias to obtain a listing of all aliases that begin with the letter g. Pipe the results to the Sort-Object cmdlet, and sort on the property attribute called definition. This is shown here:

     gal g* |sort-object -property definition

  6. The listings of cmdlets that begin with the letter g are now sorted, and the results of the command are as follows:

     CommandType     Name                            Definition -----------     ----                            ---------- Alias           gal                             Get-Alias Alias           gci                             Get-ChildItem Alias           gcm                             Get-Command Alias           gc                              Get-Content Alias           gh                              Get-Help Alias           ghy                             Get-History Alias           gi                              Get-Item Alias           gp                              Get-ItemProperty Alias           gl                              Get-Location Alias           gm                              Get-Member Alias           gps                             Get-Process Alias           gdr                             Get-PSDrive Alias           gsnp                            Get-PSSnapin Alias           gsv                             Get-Service Alias           gu                              Get-Unique Alias           gv                              Get-Variable Alias           gwmi                            Get-WmiObject Alias           group                           Group-Object

  7. Use the alias for the Get-ChildItem cmdlet and pipe the output to the alias for the Get-Member cmdlet. This is shown here:

     gci | gm

  8. To only see properties available for the Get-ChildItem cmdlet, use the membertype argument and supply a value of property. Use Tab Completion this time, rather than the gci | gm alias. This is shown here:

     get-childitem | get-member -membertype property

  9. The output from this command is shown here:

        TypeName: System.IO.DirectoryInfo Name              MemberType Definition ----              ---------- ---------- Attributes        Property   System.IO.FileAttributes Attributes {get;set;} CreationTime      Property   System.DateTime CreationTime {get;set;} CreationTimeUtc   Property   System.DateTime CreationTimeUtc {get;set;} Exists            Property   System.Boolean Exists {get;} Extension         Property   System.String Extension {get;} FullName          Property   System.String FullName {get;} LastAccessTime    Property   System.DateTime LastAccessTime {get;set;} LastAccessTimeUtc Property   System.DateTime LastAccessTimeUtc {get;set;} LastWriteTime     Property   System.DateTime LastWriteTime {get;set;} LastWriteTimeUtc  Property   System.DateTime LastWriteTimeUtc {get;set;} Name              Property   System.String Name {get;} Parent            Property   System.IO.DirectoryInfo Parent {get;} Root              Property   System.IO.DirectoryInfo Root {get;}    TypeName: System.IO.FileInfo Name              MemberType Definition ----              ---------- ---------- Attributes        Property   System.IO.FileAttributes Attributes {get;set;} CreationTime      Property   System.DateTime CreationTime {get;set;} CreationTimeUtc   Property   System.DateTime CreationTimeUtc {get;set;} Directory         Property   System.IO.DirectoryInfo Directory {get;} DirectoryName     Property   System.String DirectoryName {get;} Exists            Property   System.Boolean Exists {get;} Extension         Property   System.String Extension {get;} FullName          Property   System.String FullName {get;} IsReadOnly        Property   System.Boolean IsReadOnly {get;set;} LastAccessTime    Property   System.DateTime LastAccessTime {get;set;} LastAccessTimeUtc Property   System.DateTime LastAccessTimeUtc {get;set;} LastWriteTime     Property   System.DateTime LastWriteTime {get;set;} LastWriteTimeUtc  Property   System.DateTime LastWriteTimeUtc {get;set;} Length            Property   System.Int64 Length {get;} Name              Property   System.String Name {get;}

  10. Use the membertype argument of the Get-Member cmdlet to view the methods available from the object returned by the Get-ChildItem cmdlet. To do this, supply a value of method to the membertype argument, as follows:

     get-childitem | get-member -membertype method

  11. The output from the previous list returns all the methods defined for the Get-ChildItem cmdlet. This output is shown here:

        TypeName: System.IO.DirectoryInfo Name                      MemberType Definition ----                      ---------- ---------- Create                    Method     System.Void Create(), System.Void Creat... CreateObjRef              Method     System.Runtime.Remoting.ObjRef CreateOb... CreateSubdirectory        Method     System.IO.DirectoryInfo CreateSubdirect... Delete                    Method     System.Void Delete(), System.Void Delet... Equals                    Method     System.Boolean Equals(Object obj) GetAccessControl          Method     System.Security.AccessControl.Directory... GetDirectories            Method     System.IO.DirectoryInfo[] GetDirectorie... GetFiles                  Method     System.IO.FileInfo[] GetFiles(String se... GetFileSystemInfos        Method     System.IO.FileSystemInfo[] GetFileSyste... GetHashCode               Method     System.Int32 GetHashCode() GetLifetimeService        Method     System.Object GetLifetimeService() GetObjectData             Method     System.Void GetObjectData(Serialization... GetType                   Method     System.Type GetType() get_Attributes            Method     System.IO.FileAttributes get_Attributes() get_CreationTime          Method     System.DateTime get_CreationTime() get_CreationTimeUtc       Method     System.DateTime get_CreationTimeUtc() get_Exists                Method     System.Boolean get_Exists() get_Extension             Method     System.String get_Extension() get_FullName              Method     System.String get_FullName() get_LastAccessTime        Method     System.DateTime get_LastAccessTime() get_LastAccessTimeUtc     Method     System.DateTime get_LastAccessTimeUtc() get_LastWriteTime         Method     System.DateTime get_LastWriteTime() get_LastWriteTimeUtc      Method     System.DateTime get_LastWriteTimeUtc() get_Name                  Method     System.String get_Name() get_Parent                Method     System.IO.DirectoryInfo get_Parent() get_Root                  Method     System.IO.DirectoryInfo get_Root() InitializeLifetimeService Method     System.Object InitializeLifetimeService() MoveTo                    Method     System.Void MoveTo(String destDirName) Refresh                   Method     System.Void Refresh() SetAccessControl          Method     System.Void SetAccessControl(DirectoryS... set_Attributes            Method     System.Void set_Attributes(FileAttribut... set_CreationTime          Method     System.Void set_CreationTime(DateTime v... set_CreationTimeUtc       Method     System.Void set_CreationTimeUtc(DateTim... set_LastAccessTime        Method     System.Void set_LastAccessTime(DateTime... set_LastAccessTimeUtc     Method     System.Void set_LastAccessTimeUtc(DateT... set_LastWriteTime         Method     System.Void set_LastWriteTime(DateTime ... set_LastWriteTimeUtc      Method     System.Void set_LastWriteTimeUtc(DateTi... ToString                  Method     System.String ToString()

  12. Use the up arrow key to retrieve the previous Get-ChildItem | Get-Member -MemberType method command, and change the value method to m* to use a wild card to retrieve the methods. The output will be exactly the same as the previous listing of members because the only membertype beginning with the letter m on the Get-ChildItem cmdlet is the MemberType method. The command is as follows:

     get-childitem | get-member -membertype m*

  13. Use the -inputobject argument to the Get-Member cmdlet to retrieve member definitions of each property or method in the list. The command to do this is as follows:

     get-member -inputobject get-childitem

  14. The output from the previous command is shown here:

     PS C:\> get-member -inputobject get-childitem    TypeName: System.String Name             MemberType            Definition ----             ----------            ---------- Clone            Method                System.Object Clone() CompareTo        Method                System.Int32 CompareTo(Object value),... Contains         Method                System.Boolean Contains(String value) CopyTo           Method                System.Void CopyTo(Int32 sourceIndex,... EndsWith         Method                System.Boolean EndsWith(String value)... Equals           Method                System.Boolean Equals(Object obj), Sy... 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, Int3... IndexOfAny       Method                System.Int32 IndexOfAny(Char[] anyOf,... Insert           Method                System.String Insert(Int32 startIndex... IsNormalized    Method                System.Boolean IsNormalized(), System... LastIndexOf      Method                System.Int32 LastIndexOf(Char value, ... LastIndexOfAny   Method                System.Int32 LastIndexOfAny(Char[] an... Normalize       Method                System.String Normalize(), System.Str... PadLeft          Method                System.String PadLeft(Int32 totalWidt... PadRight         Method                System.String PadRight(Int32 totalWid... Remove           Method                System.String Remove(Int32 startIndex... Replace          Method                System.String Replace(Char oldChar, C... Split            Method                System.String[] Split(Params Char[] s... StartsWith       Method                System.Boolean StartsWith(String valu... Substring        Method                System.String Substring(Int32 startIn... ToCharArray      Method                System.Char[] ToCharArray(), System.C... ToLower          Method                System.String ToLower(), System.Strin... ToLowerInvariant Method                System.String ToLowerInvariant() ToString         Method                System.String ToString(), System.Stri... ToUpper          Method                System.String ToUpper(), System.Strin... ToUpperInvariant Method                System.String ToUpperInvariant() Trim             Method                System.String Trim(Params Char[] trim... TrimEnd          Method                System.String TrimEnd(Params Char[] t... TrimStart        Method                System.String TrimStart(Params Char[]... Chars            ParameterizedProperty System.Char Chars(Int32 index) {get;} Length           Property              System.Int32 Length {get;}

  15. This concludes the procedure for using the Get-Member cmdlet.

    image from book
    Quick Check

    Q. To retrieve a listing of aliases beginning with the letter g that is sorted on the definition property, what command would you use?

    A. gal g* | sort-object -property definition

    image from book




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net