While I have already introduced get-Member, this page goes into more detail. 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, you can type: getobjectXYZ | 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.
1. The Concept behind get-Member
The role of get-Member is to discover 'handles' so that we can achieve a scripting task. These 'handles' or memberTypes are split into methods and properties. Suitable objects to research with 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:
- get-Service
Results in a long list of Windows services - get-Service | get-Member
Results in a rich list of properties and methods - get-Service messenger
Reveals that the service Messenger has a property called 'Status' with a value of 'Stopped' (or
'Running'). - get-Service messenger | get-Member
Reveals more properties specific to the messenger service
TIP: Try 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
2. The Basics of get-Object | 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.
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 getobject, remember the 'Pipe' or 'Pipeline' symbol. This vertical line | is ASCII 124 and looks like this at the PS Prompt |
Correct Sequence: get-object | get-Member
Incorrect Syntax: get-Member | get-object (wrong sequence of verb-noun pair)
Trap: get-Member get-object (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.
TIP: Try 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.
3. 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, since 'groupobject' 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.
4. 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.
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-Object | get-Member.