Condividi tramite


Using the Get-Member Cmdlet

Listing the Properties and Methods of a Command or Object

Any time you’re writing scripts or working from the command line the biggest problem you face is this: how do I know what I can and cannot do? How do I know which properties and methods are available to me? How can I actually write a script or type a command-line command without having to memorize every object model found on MSDN?

One way to do that is to use the Windows PowerShell Get-Member cmdlet. Once you connect to an object you can pipe that object to Get-Member; in turn, Get-Member will enumerate the properties and methods of that object. For example, suppose you’d like to know which properties and methods are available for event logs. Assuming you know that Get-EventLog -list will retrieve an event log object, all you need to do is issue this command:

Get-EventLog -list | Get-Member

In turn, Windows PowerShell will report back data similar to this:

Name                      MemberType Definition
----                      ---------- ----------
add_Disposed              Method     System.Void add_Disposed(EventHandler v...
add_EntryWritten          Method     System.Void add_EntryWritten(EntryWritt...
BeginInit                 Method     System.Void BeginInit()
Clear                     Method     System.Void Clear()
Close                     Method     System.Void Close()

Interested in the properties of the WMI Win32_BIOS class? Then use this command, tacking on the -membertype parameter in order to limit returned data to properties:

Get-WmiObject win32_bios | Get-Member -membertype properties

And, yes, setting -membertype to methods returns just the methods.

Here’s an interesting use of Get-Member. Did you know that some of the properties returned by the Get-Process cmdlet have aliases? Well, you would if you ran this command, setting -membertype to AliasProperty:

Get-Process | Get-Member -membertype aliasproperty

Here’s what you get back:

Name    MemberType    Definition
----    ----------    ----------
Handles AliasProperty Handles = Handlecount
Name    AliasProperty Name = ProcessName
NPM     AliasProperty NPM = NonpagedSystemMemorySize
PM      AliasProperty PM = PagedMemorySize
VM      AliasProperty VM = VirtualMemorySize
WS      AliasProperty WS = WorkingSet

What does it mean for a property to have an alias? That simply means that the next time you work with the Get-Process cmdlet you can type NPM rather then NonpagedSystemMemorySize. For example:

Get-Process | Select-Object name, npm

And, yes, Get-Member does work with COM objects as well. Need to know the properties and methods of the FileSystemObject? All you have to do is use the New-Object cmdlet to create an instance of the FileSystemObject and then pipe that object to Get-Member:

New-Object -com scripting.filesystemobject | Get-Member

Here’s the type of information you’ll get back:

Name                MemberType Definition
----                ---------- ----------
BuildPath           Method     string BuildPath (string, string)
CopyFile            Method     void CopyFile (string, string, bool)
CopyFolder          Method     void CopyFolder (string, string, bool)
CreateFolder        Method     IFolder CreateFolder (string)
CreateTextFile      Method     ITextStream CreateTextFile (string, bool, bool)
DeleteFile          Method     void DeleteFile (string, bool)
DeleteFolder        Method     void DeleteFolder (string, bool)
DriveExists         Method     bool DriveExists (string)
FileExists          Method     bool FileExists (string)
FolderExists        Method     bool FolderExists (string)
Get-Member Aliases
  • gm