Interpreting the cmdlet syntax

The syntax section of the cmdlet help can be overwhelming at first, so let's drill into it a bit and try to understand it in detail. I will use the get-service cmdlet for an example, but the principles are same for any cmdlet:

Interpreting the cmdlet syntax

The first thing to notice is that there are three different get-service calls illustrated here. They correspond to the PowerShell concept of ParameterSets, but you can think of them as different use cases for the cmdlet. Each ParameterSet, or use case will have at least one unique parameter. In this case, the first includes the –Name parameter, the second includes –DisplayName, and the third has the –InputObject parameter.

Each ParameterSet lists the parameters that can be used in a particular scenario. The way the parameter is shown in the listing, tells you how the parameter can be used. For instance, [[-Name] <String[]>] means that the name parameter has the following attributes:

  • It is optional (because the whole definition is in brackets)
  • The parameter name can be omitted (because the name is in brackets)
  • The parameter values can be an array (list) of strings (String[])

    Tip

    Many cmdlet parameters allow lists to be passed, rather than only a single value. Taking the time to check the types of parameters can make it simpler and quicker to write scripts, if you can eliminate looping because you are able to pass all the values at once.

In general, a parameter will be shown in one of the following forms:

Applying this to the Get-Service syntax shown previously, we see that some valid calls of the cmdlet could be:

  • Get-Service WinRM
  • Get-Service –Name WinRM
  • Get-Service *SQL* -Exclude SQLWriter
  • Get-Service aspnet_state,W3SVC

    Tip

    Your turn!

    Read the help for another cmdlet (get-process is a good candidate) and work through the syntax until you understand how the parameters are specified.