The parameters of PowerShell cmdlets

I. Introduction

Now that you know what a PowerShell cmdlet is, and are familiar with the syntax based on the association of a verb and a noun, we'll move on to the notion of parameters. Once we've seen what a parameter is, we'll look at parameter syntax and how to use one or more parameters with a PowerShell cmdlet.

II. PowerShell cmdlet: what is a parameter?

A parameter in a PowerShell cmdlet is a value that you can pass to the cmdlet to modify its behavior. To make things clearer, let's take an example. With the"Get-Process" cmdlet, you can use the"-Name" parameter to specify the name of the process you wish to obtain.

The complete command might look like this, taking the process name "explorer" as an example:

Get-Process -Name explorer

Without a parameter,"Get-Process" will return a list of all active processes on the machine. With the"-Name" parameter, we'll modify the cmdlet's behavior to retrieve only the list of processes whose name is"explorer".

Note : parameters are available not only for PowerShell cmdlets, but also for scripts and functions.

III. Parameter syntax

As you may already know, the syntax of a parameter consists of a name and a value. Which gives:

Verbe-Nom -NomParamètre ValeurParamètre

Remember one thing: a parameter name is always preceded by a hyphen, unlike the value associated with that parameter. When writing a command, the cmdlet name can be followed by a single parameter or by several parameters, as required.

Verbe-Nom -Paramètre1 Valeur1 -Paramètre2 Valeur2 -Paramètre3 -Valeur3

In a nutshell:

powershell cmdlet syntax with one parameter

There are several types of parameter , and each type of parameter accepts different input values: strings, integers, Booleans, objects and so on.

IV. How do I obtain a list of parameters for a PowerShell cmdlet?

Although there are common parameters between cmdlets, each cmdlet is likely to have its own list of parameters. We can't memorize the list of parameters for all the cmdlets we use, so how can we? The answer lies in PowerShell's help function.

First of all, we can mention the"CTRL + Space" keyboard shortcut mentioned in a previous chapter. Simply write the following in the console:

Get-Service -

Then press"CTRL + Space" to obtain a list of all available parameters, with the parameter type. Then use the arrow keys to select, and the Enter key to confirm.

Keyboard shortcut to list powershell cmdlet parameters

However, this method, while practical, doesn't provide much information. What is the purpose of this parameter? Is it mandatory? Etc... A number of questions remain unanswered.

To obtain additional information, we're going to use the"Get-Help" cmdlet, whose purpose is to provide help, in the console, about another PowerShell cmdlet. We'll specify the"-Detailed" parameter to obtain more complete help.

Here is the command to execute:

Get-Help -Name Get-Service -Detailed
# Que l'on peut raccourcir de cette façon :
Get-Help Get-Service -Detailed

In the returned result, we have a section with all the parameters, where we have each time the name of the parameter, its type and a description. In addition, we have examples and further information on the "Get-Service" cmdlet.

For detailed information on cmdlet parameters only, we can use this command:

Get-Help -Name Get-Service -Parameter *
# Ou pour un paramètre spécifique, par exemple Name :
Get-Help -Name Get-Service -Parameter Name

Here's an example:

Get-Help - List all powershell cmdlet parameters

I'd like to draw your attention to one particular line:

Required? false / true

This indicates whether or not this parameter is mandatory when using this cmdlet. When set to"true", the parameter is mandatory. When set to"false", the parameter is not mandatory. We can also see that this parameter has no default value:"Default value: None".

Furthermore,"Accept widlcard characthers?" indicates whether or not this cmdlet's parameter accepts the wildcard character "*" as a value. As for"Accept pipeline input?", this indicates whether the parameter accepts values received by the pipeline, but we haven't mentioned this notion.

V. The name and position of PowerShell parameters

Finally, on the subject of parameters, it's essential to look at the status of the"Position" parameter associated with each parameter, since it can have several values. Here, you should refer to this value:

The three most frequent values are :

  • Named : this is a named parameter, i.e. it must be called by its name and the value associated with it must be preceded by the parameter name.
  • 0: parameter is expected in first position
  • 1 : parameter is expected in second position

In fact, if we look at the characteristics of the"Path" parameter of the"Get-ChildItem" cmdlet (used to list the contents of a directory), we can see that it has a different configuration. It is expected to be in the first position when using this cmdlet (Position? 0) and has an interesting default value: the current directory (vis-à-vis the PowerShell prompt).

Get-Help - Get-ChildItem - Path parameter - Example

There are named parameters and positional parameters, but we recommend always specifying the parameter name followed by its value to make it more explicit and avoid misinterpretation.

VI. Conclusion

By now, you're familiar with the syntax of a PowerShell cmdlet, with or without parameters, and you should be able to fully understand the purpose of parameters. In the next chapter, we'll take a closer look at the PowerShell help system.


book to learn PowerShell