Common Parameters

The common parameters are a set of cmdlet parameters that you can use with any cmdlet. They're implemented by PowerShell, not by the cmdlet developer, and so they are automatically available to any cmdlet.

The 11 common parameters with their aliases in parentheses:

-Debug (-db)
-ErrorAction (-ea)
-ErrorVariable (-ev)
-InformationAction (-infa)
-InformationVariable (-iv)
-OutBuffer (-ob)
-OutVariable (-ov)
-PipelineVariable (-pv)
-Verbose (-vb)
-WarningAction (-wa)
-WarningVariable (-wv)

Full details below with default values underlined.

   -Debug         Provide programming-level information about the operation [boolean]
                  $true (-Debug:$true). Has the same effect as -Debug.
                  By default, debugging messages aren’t displayed because the value of the $DebugPreference
                  variable is SilentlyContinue.
                  In interactive mode, the Debug parameter overrides the value of the $DebugPreference variable
                  for the current command, setting the value of $DebugPreference to Inquire.
                  In non-interactive mode, the Debug parameter overrides the value of the $DebugPreference variable
                  for the current command, setting the value of $DebugPreference to Continue.

                  -Debug:$true has the same effect as -Debug.
                  Use -Debug:$false to suppress the display of debugging messages when $DebugPreference isn’t
                  SilentlyContinue, which is the default.

   -ErrorAction   Control command behavior when an error occurs [enum]
                  Valid values: Continue , Stop, Suspend, SilentlyContinue, Ignore, Inquire. [Enum]
                  e.g. -EA SilentlyContinue  will supress errors from being printed to stderr.

                  The -SilentlyContinue option will cause any Throw statement to be completely ignored.
                  This can be mitigated by using Try/Catch or by adding a trap {} statement to trap the error.
                  [ Set a default with the optional Preference Variable $ErrorActionPreference = Continue ]

   -ErrorVariable Name of variable (in addition to $error) in which 
                  to place objects to which an error has occurred [string]

   -InformationAction
                  Override the value of the $InformationPreference preference variable.
                  Valid values: SilentlyContinue, Stop, Continue, Inquire, Ignore, Suspend 
                  [ Set a default with the optional Preference Variable $InformationPreference = SilentlyContinue ]

   -InformationVariable [+]variable_name    
                  Store into a variable a string that you specify with Write-Information. 
                  PowerShell 5.0+

   -OutBuffer     The number of objects to buffer before calling the next 
                  cmdlet in the pipeline.[Int32]
                          
   -OutVariable   Name of variable in which to place output objects [string]
                  (equivalent to piping the command Set-Variable -passthru true)

   -PipelineVariable
                  Stores the value of the current pipeline element as a variable, for
                  any named command as it flows through the pipeline.
                  Valid values are strings, the same as for any variable names.

   -Verbose       Provide detailed information about the operation [boolean]
                    $true (-Verbose:$true) has the same effect as -Verbose.
                    $false (-Verbose:$false)
                  [ Set a default with the optional Preference Variable $VerbosePreference = $True ]

   -WarningAction Determines how the cmdlet responds to a warning from the command.
                  This parameter works only when the command generates a warning message.
                  For example when a command contains the Write-Warning cmdlet.

                  -WarningAction:Continue
                  Display the warning messages and continues executing the command. Continue is the default.

                  -WarningAction:SilentlyContinue
                  Suppress the warning message and continue executing the command.

                  -WarningAction:Inquire
                  Display the warning message and prompts you for confirmation before continuing execution.
                  This value is rarely used.

                  -WarningAction:Stop
                  Display the warning message and stop executing the command.
                     Continue, SilentlyContinue, Inquire, Stop
                  [ Set a default with the optional Preference Variable $WarningPreference = Continue ]

   -WarningVariable
                  Stores warnings about the command in the specified variable. (wv)
                  All generated warnings are saved in the variable even if the
                  warnings are not displayed to the user.
                  To append the warnings to the variable content, instead of replacing
                  any warnings that might already be stored there, type a plus sign (+)
                  before the variable name.  -WarningVariable +myvar

   -Write-Information
                  values are shown depending on the value of the -InformationAction
                  common parameter; if you do not add the -InformationAction common parameter,
                  Write-Information strings are shown depending on the value of the $InformationPreference

An Error Variable can be useful for logging errors as part of a pipeline process i.e. when only a few out of many items may fail:

some-pscmdlet | foreach {
  another-pscmdlet -options blah -ErrorVariable ErrFlag
  if ($ErrFlag) { Echo "$_.name failed" }
}

Risk Mitigation Parameters

In addition to the common parameters, many cmdlets offer risk mitigation parameters. Cmdlets that involve risk to the system or to user data usually offer these two parameters:

-Confirm (-cf)
-WhatIf (-wi)

   -Confirm       Prompt the user for permission before performing any action that
                  modifies the system.[boolean] (cf)
                  -Confirm:$false or -Confirm:$true
[ Set a default with the optional Preference Variable $ConfirmPreference = $true ] -WhatIf Explain what will happen if the command is executed, without actually executing the command.[boolean] (wi) -whatif:$false or -whatif:$true [ Set a default with the optional Preference Variable $WhatIfPreference = 0 ]

The -confirm parameter allows dropping into the runtime command line while running a PowerShell scriptCmdlet:
The confirmation prompt is [Y] Yes [A] All [N] No [L] No to all [S] Suspend
choosing S will drop you at the command prompt where you can echo variables, make changes etc before typing EXIT to resume running the scriptCmdlet.

Default values

Other than where stated above, none of the common parameters have a defined default value, so they will evaluate as FALSE.

You can set a default value, for the duration of a script or a session using a Preference Variable, for example you could set the value of the WhatIf preference to TRUE and this will affect every subsequent cmdlet called:

PS C:\demo\> $WhatIfPreference=$true
PS C:\demo\> new-item 'Testing'
What if: Performing the operation "create File" on target "Destination: C:\demo\Testing"

Alternatively the $PSDefaultParameterValues preference variable lets you specify custom default values for just one specific cmdlet or advanced function, see Help about_Parameters_Default_Values for more.

Any Preference Variable values that you set will be specific to the current PowerShell session. To make variables effective in all PowerShell sessions, add them to your PowerShell profile.

Passthru

The -passthru parameter is not a common parameter but is present on a large number of cmdlets. It allows outputting an object when the cmdlet would not do so by default.

e.g. Start-Process, Stop-Service, Stop-Job, do not by default, return an object to the pipeline they just complete the action and return nothing.

If you start a new process using Start-Process and want the script to later close that process, it can be useful to save the process object with -Passthru and then manipulate that object later on:

$processObject = Start-Process notepad.exe -PassThru
# other code here
Stop-Process -InputObject $processObject

Some common parameters will have no effect in some cmdlets, this does not raise an error.

Examples

Delete a file and capture any errors in a variable:

PS C:\> Del $somefile -ErrorVariable somevariable

Notice that the error variable is not prefixed with a $ here, using $somevariable will not work.

If the ErrorVariable name is prefixed with a + then PowerShell will ADD the errors to that variable:

PS C:\> Del $somefile -ErrorVariable +somevariable
PS C:\> $somevariable.count

“Friendship is born at that moment when one person says to another, What! You, too? I thought I was the only one” ~ C. S. Lewis

Related PowerShell Cmdlets

Preference variables - Verbosity, Confirmations, ErrorAction, $Debug
Parameters - Command Line Parameters param() and $args[]
get-help - Open the help file, list parameters for a cmdlet.


 
Copyright © 1999-2024 SS64.com
Some rights reserved