Skip navigation

PowerShell's Common Parameters

Take a look at thehelp for any PowerShell cmdlet and you'll notice <CommonParameters> at the end of each one's syntax list. What's that mean?

PowerShell has a set of parameters that the shell itself adds to every cmdlet. In other words, the developer of a cmdlet doesn't need to code these - they're just there, like magic. They're also added to the scripts and functions you write in the shell. You can learn about the complete set by running help about_common_parameters in the shell, but here's a quick rundown of the most frequently-used ones.

-Verbose

This switch sets the shell's $VerbosePreference variable to "Continue" for the cmdlet, script, or function, meaning that any embedded writes to the Verbose pipeline (a la Write-Verbose) will display output. There's no guarantee that a cmdlet will actually produce verbose output, but the -Verbose switch will enable whatever's in there.

-Debug

Works a lot like -Verbose, setting the $DebugPreference variable. 

-OutVariable

This parameter takes a variable name. Now, keep in mind that variable names do not include the $. The idea is that the cmdlets' output will be captured into whatever variable you specify. So, to capture a list of services into the variable $MyServices, and then display the contents of that variable, you'd do this:
Get-Service -outvariable myservices
$myservices

You need to use some caution here. Consider this wrong example:
$x = 'hello'
Get-Service -outvariable $x
$x

Here, the services will be stored in a variable $hello. That's because I put the dollar sign in front of x when I passed it to -OutVariable. The dollar sign makes PowerShell use the contents of the variable. This one's easy to mess up, and can be very confusing if you do.

-ErrorVariable

Works a lot like -OutVariable, accepting a variable name (no dollar sign!). Any errors that occur while running the command will be stored in this variable for you.

-ErrorAction

Accepts one of these values: Stop, Continue, SilentlyContinue, Inquire. This is a key trick to error handling, changing the command's default behavior when it encounters an error.


Again, you can read more about these and the other common parameters in PowerShell's help.
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish