Parameters, Values and Switches
A parameter tells a command more about what you want, written as a dash and a name with a value after it, or as a switch with no value, and Tab and Ctrl+Space show you every parameter a command has.
Novice · PowerShell · Updated
After this lesson you can
- Give a command a parameter with a value, a switch, or a positional value, with quotes around a value that has spaces
- Find a command's parameters with Tab and Ctrl+Space
Key takeaways
- A parameter is a dash and a name, such as
-Name, followed by its value. A value with spaces in it goes in quotes. - A switch is a parameter with no value, such as
-NameonGet-ChildItem. Writing it turns it on. - A value with no parameter name in front goes to the command's first position, so
Get-Service AudiosrvmeansGet-Service -Name Audiosrv. - Type a dash after a command and press Tab to go through its parameters, or Ctrl+Space to see them all.
Your computer's sound stops working. Someone at the IT help desk says, "Check whether the Windows Audio service is running." Get-Service lists every service on the computer, usually two or three hundred of them, and scrolling for one is slow. You need to tell the command which one you mean. Ordering coffee works the same way: "a coffee" gets you something, but "a large latte with oat milk" gets you what you want. The extra details are the command's parameters.
A parameter and its value
A parameter is a dash and a name, written after the command, then a space and its value. -Name is read "dash name":
Get-Service -Name Audiosrv
Status Name DisplayName
------ ---- -----------
Running Audiosrv Windows Audio
Running: the service is fine, so the problem is somewhere else. Audiosrv is the service's short name. Its long name, the one Windows shows in its Services window, is its display name, and Get-Service has a parameter for that too. That value has a space in it, so it goes in quotes:
Get-Service -DisplayName 'Windows Audio'
Status Name DisplayName
------ ---- -----------
Running Audiosrv Windows Audio
Single quotes and double quotes both work. Without quotes, PowerShell reads Windows and Audio as two separate values, and stops with an error that names the one it didn't expect:
Get-Service -DisplayName Windows Audio
Get-Service: A positional parameter cannot be found that accepts argument 'Audio'.
A parameter can also take several values, with commas between them: Get-Service -Name Audiosrv, Spooler shows both services.
Switches: on by being there
Some parameters take no value at all. Writing one turns it on, like the "extra hot" button on a coffee machine. These are switches.
Get-ChildItem lists what's inside a folder (Looking Around: Get-ChildItem, in the next chapter, teaches it in full). Windows keeps its desktop backgrounds in C:\Windows\Web\Wallpaper. Here -Path takes a value, the folder, and -Name is a switch that asks for the names only:
Get-ChildItem -Path C:\Windows\Web\Wallpaper -Name
Extended
Spotlight
ThemeA
ThemeB
ThemeC
ThemeD
Windows
Your list may differ a little with your version of Windows. Leave -Name off and you get a fuller table, with dates and sizes. Order doesn't matter: -Name could come first.
Leaving the name out: positional values
Typing -Name every time gets old, and PowerShell lets you skip it for a command's most-used parameters. A value with no parameter name in front of it is a positional value: PowerShell gives it to the parameter in the first position, which for Get-Service is -Name:
Get-Service Audiosrv
Status Name DisplayName
------ ---- -----------
Running Audiosrv Windows Audio
That's what Get-Process explorer did in the last lesson. For Get-ChildItem, the first position is -Path, so Get-ChildItem C:\Windows\Web\Wallpaper -Name works too. Positional values are handy at the prompt. In anything other people will read, write the names out: -Name Audiosrv says what it is. Reading Help: Get-Help shows how to find out which parameters have a position.
Let Tab and Ctrl+Space show you
You don't have to know a command's parameters by heart. Type Get-Service - (with the dash) and press Tab: PowerShell writes -Name. Press Tab again for -DependentServices, again for the next one, and Shift+Tab to go back.
Press Ctrl+Space instead to see every parameter at once, in a menu under the prompt. Pick one with the arrow keys and press Enter.
Tab finishes values too: Get-Service -Name Aud and Tab gives AudioEndpointBuilder, and Tab again Audiosrv. If you mistype a parameter's name, PowerShell names the one it couldn't find:
Get-Service -Nmae Audiosrv
Get-Service: A parameter cannot be found that matches parameter name 'Nmae'.
On a Mac or Linux, Tab works as it does in bash: it fills in as much as all the matches share, and a second press lists them. Ctrl+Space shows the menu, unless your Mac uses Ctrl+Space to switch keyboard languages. Windows PowerShell 5.1 has the same keys.
Try it. Type Get-Process - and press Ctrl+Space. Find the parameter that takes a process's Id.
Check yourself
2 questions to try on the lesson page.
So what
Parameters turn a command that does something general into one that does exactly what you meant, and Tab and Ctrl+Space mean you never have to guess what they're called.