PowerShell


Windows PowerShell Get-Member

Introduction to Windows PowerShell Get-Member

Get-Member is an essential command for discovering more about PowerShell's objects.  Because you are at the command line, you cannot right click an object and check its properties; instead, what you can do is type: get-ObjectXYZXYZ | get-Member.

PowerShell is sometimes referred to as a self-describing language.  Indeed, it is thanks to get-Member that we can see the properties and methods associated with any given object.

Topics for PowerShell's get-Member

The Concept behind get-Member

The reason that I use get-Member at every opportunity is so that I can discover 'handles', which help me achieve a particular scripting task.  These 'handles' or memberTypes are split into methods and properties.  Suitable objects to learn more about get-Member include, service, process, eventlog, or WmiObject.

Employing get-Member is a useful tactic in the bigger game of scripting specific properties.  For example, if your goal is to stop, or to start a service, then you need to investigate the scripting properties, and possible values, for that service.  A little research with get-Member will reveal a property called 'Status', with values of 'Running' or 'Stopped' - perfect for our task.

Here are three examples to illustrate my research technique:

1) get-Service 
Results in a long list of Windows services

2) get-Service | get-Member 
Results in a rich list of properties and methods

3) get-Service messenger 
Reveals that the service Messenger has a property called 'Status' with a value of 'Stopped' (or 'Running').

3a) get-Service messenger | get-Member
Reveals more properties specific to the messenger service

Special note, the pipeline symbol displays as ¦ in PowerShell, but as | in notepad.

PowerShell Tip - use the tab for Auto-completionTry judicious use of 'tab' and invoke Auto-completion, for example try, get-p [Tab].  If you press tab again PowerShell cycles through commands beginning with get-p, for example get-Process, get-PSDrive

The Basics of get-ObjectXYZ | get-Member

Naturally, the phrase get-Member never varies, a case of learn once and apply to many objects.  Just remember the hyphen, and also remember that there are no spaces in verb-noun pairs.  Please note that get-ObjectXYZ is just a generic name that I made up to illustrate get-Member in action.

Correct Syntax:  get-Member

Incorrect Syntax: get member (no hyphen), or get -Member or even get- member (spaces)

As I mentioned earlier, when you use get-Member in conjunction with other commands such as get-ObjectXYZ, remember the 'Pipe' or 'Pipeline' symbol.  This vertical line | is ASCII 124 and looks like this at the PS Prompt  ¦

Correct Sequence: get-ObjectXYZ | get-Member 

Incorrect Syntax: get-Member | get-ObjectXYZ (wrong sequence of verb-noun pair)
Trap:  get-Member get-ObjectXYZ (Forgot the pipe |)

More Examples of get-Member

get-WmiObject Win32_processor | get-Member
Note:  get-Member is even more useful with WmiObject because this type of object varies more than objects such as Service, Process or Eventlog.

PowerShell Tip - Try alias such as gwmiTry aliases, for example gwmi for get-WmiObject.  Many people use gm instead of get-Member.

get-Process |get-Member

get-Eventlog system | get-Member
Note:  If you don't tell PowerShell which eventlog you want, the command does not complete.

Filter with -Membertype

Results from the simple command: get-Member, may produce too many MemberTypes; if so, then you can filter the output with a -Membertype argument, for example:

get-Process | get-Member -Membertype property
or
get-Process | get-Member -Membertype method

Appreciate that the results of the parameter -MemberType are grouped into at least four categories:  AliasProperty, Method, Property and ScriptProperty. Once you have researched -MemberType, you may see new applications for the properties that it reveals, for example
get-Process | group company

Strictly speaking, the above command should be, 'group-Object company', however, 'group-Object' has an alias, you can shorten the command to plain 'group'.

Here is a method for expanding the company information:
get-Process | sort company |format-Table -group company name, description -autosize

Incidentally, this whole page of -Member examples gives valuable experience of when to use the hyphen - also called a dash, and sometimes referred to as a minus sign.  For example -Membertype takes the hyphen prefix, whereas property, as in -Membertype property does not need any prefix.

  ˚

Getting Help for get-Member

For a complete list of filters supported by the get-Member command, call for help thus:
help get-Member.

This may be just me, but I have the urge to call this -method instead of -Member.  I mention this because you always learn more from mistakes.  The answer lies in, a) Reading the error message!  b) Trying an example you know works, for example, I was trying this:

Here is a mistake that I made:
get-WmiObject win32_computersystem | get-method

When I read the error message it said: 'get-method is not recognised'.  Hmmm... I thought, let me try an old friend the process object.

When I tried
get-Process | get-method and this also failed, I realized that I was having a 'senior moment'.  Fortunately I woke up and read the error message slowly. 'get-method is not recognised'.   Ah ha, get-method is what's wrong, why don't I try get-Member.  Perfect, it worked just as I wanted.  Thank you error message.

get-help and get-Member working in tandem.

Remember that get-help and get-Member are complimentary.  On the one hand, get-help will disclose information about parameters or switches that you can employ with your command, for example -recurse with get-Childitem -path and -pattern with Select-String. 

On the other hand, get-Member will disclose the properties and methods, for example .extension .fullname both of which are useful with get-Childitem.  My point is that get-help and get-Member each provide different information; they are complimentary rather than interchangeable.  If you are complete beginner, investigate both get-help and get-Member; when you are an intermediate don't get fixated on one, and forget all about the other.

More examples of get-Member

Summary of PowerShell's get-Member

When you need to investigate the methods and properties available to a PowerShell object, then call for get-Member. You will soon get used its hyphen and associated 'Pipe' symbol (|), just remember the correct sequence:
get-ObjectXYZ | get-Member.

See Also

Windows PowerShell Home  • Introduction  • Cmdlets  • Exchange 2007  • Profile.ps1  • $_.Pipeline

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.