- Basic PowerShell Command Structure
- <tt>Get-Command</tt>
- <tt>Get-Help</tt>
- <tt>Get-Member</tt>
- Wrapping Up
Get-Member
All PowerShell output consists of objects, which are data structures that have two essential characteristics:
- Properties: Descriptive attributes of an object
- Methods: Actions that the object can perform
As your PowerShell knowledge broadens and deepens, you’ll pipe more and more output to Get-Member (aliased to gm) to see exactly what that output is and what useful possibilities it can offer you:
Get-Service | Get-Member
The TypeName tells you what kind of .NET data type the given command outputs to the pipeline. In the case of Get-Service, that command outputs ServiceController objects.
To retrieve a list of commands that accept ServiceController objects as input (important knowledge for advanced PowerShell pipeline troubleshooting), you can run this admittedly complex statement:
Get-Command -ParameterType (((Get-Service)[0]).PSTypeNames)
In my work, I use object properties far more often than methods. We can shorten Get-Member output by filtering only on properties:
Get-Service | Get-Member -MemberType Properties
We access properties by using what’s called dot notation. For instance, to get the current run status of the Spooler service, we can do this:
$s = Get-Service -Name Spooler ; $s.Status
In the previous example, I store the Spooler object in a variable named $s. The semicolon (;) lets us issue more than one PowerShell command in the same pipeline section.
We can invoke methods in the same way. The following example reuses the $s variable to start the Spooler service:
$s.Start()