PowerShell’s common parameters are a set of parameters that are automatically available to all cmdlets and advanced functions. These parameters provide a consistent way to control the behavior of cmdlets and functions, making it easier to manage and debug scripts. In this post, we will look at the most commonly used common parameters and how to use them.

What are Common Parameters?

Common parameters are a predefined set of parameters that are included in every cmdlet and advanced function. They are automatically added by the PowerShell engine and do not need to be explicitly defined in your scripts. These parameters allow you to control various aspects of cmdlet execution, such as (but not limited to) output formatting, error handling, and debugging.

List of Common Parameters

Here are some (but not all) of the (most commonly used) common parameters:

-Verbose

Provides detailed information about the operation being performed.

-Debug

Provides debugging information.

-ErrorAction

Specifies how the cmdlet should respond to an error.

-WarningAction

Specifies how the cmdlet should respond to a warning.

-InformationAction

Specifies how the cmdlet should respond to an information event.

-ErrorVariable

Specifies a variable to store error messages.

-WarningVariable

Specifies a variable to store warning messages.

-InformationVariable

Specifies a variable to store information messages.

-OutVariable

Specifies a variable to store output data.

-OutBuffer

Specifies the number of objects to buffer before calling the next cmdlet in the pipeline.

-PipelineVariable

Specifies a variable to store the current pipeline object.

Using Common Parameters

Let’s look at some examples of how to use common parameters in your PowerShell scripts.

Verbose

The -Verbose parameter provides detailed information about the operation being performed. This can be useful for troubleshooting and understanding the behavior of your scripts.

Get-Process -Name "powershell" -Verbose

ErrorAction

The -ErrorAction parameter specifies how the cmdlet should respond to an error. The possible values are ContinueStopSilentlyContinueInquire, and Ignore.

Get-Item "C:\NonExistentFile.txt" -ErrorAction SilentlyContinue

ErrorVariable

The -ErrorVariable parameter specifies a variable to store error messages. This allows you to capture and handle errors more effectively.

Get-Item "C:\NonExistentFile.txt" -ErrorVariable myError
if ($myError) {
    Write-Host "An error occurred: $($myError[0].Exception.Message)"
}

Using common parameters in your own functions

So how can you use these common parameters in your own functions? you have to add [CmdletBinding()] to your function like this:

Function Show-Example {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        $ParameterOne
    )

    Write-Verbose "The value of ParameterOne is $ParameterOne"
    Write-Output "$ParameterOne"
}

Adding this will make your function an advanced function.

Try running the above example with the -Verbose parameter like this:

Show-Example -ParameterOne "Example" -Verbose

The result should look like this:

Show-Example -ParameterOne "Example" -Verbose
VERBOSE: The value of ParameterOne is Example
Example

Note that ‘common’ parameters like -WhatIf and -Confirm are not available by just adding [CmdletBinding()] to your function. Those (and other options) will be discussed in a different post as they require more understanding.

Preference variables

With the use of preference variables you can change the behaviour of the common parameters. for example you can use the $ErrorActionPreference variable to change how the -ErrorAction parameter works when your script encounters an error.

Say you want to silence the error output for your script, you can do this as shown below.

# Lines at the start of the script
$ErrActPref = $ErrorActionPreference
$ErrorActionPreference = "SilentlyContinue"

Get-Item "C:\NonExistentFile.txt"

# last line of the script (but before returning any output)
$ErrorActionPreference = $ErrActPref

This will cause all cmdlets between the 2nd and the last line to not show errors and continue running the script. By storing the $ErrorActionPreference variable in a different variable at the top of the script we set the new value and safely restore the old value at the end of the script. If we do not restore the value any action after this script will also use the value we set here (in this case SilentlyContinue).

Conclusion

PowerShell’s common parameters provide consistent way to control the behavior of cmdlets and functions. By understanding and using these parameters, you can create more robust and maintainable scripts. Whether you need detailed output, error handling, or debugging information, common parameters have you covered.

One response to “Understanding PowerShell’s Common Parameters”

  1. Harm Veenstra Avatar

    [CmdletBinding()] is something you need to add to your script to use Common Parameters like Verbose, Debug, etc… But…

    C:\Users\HarmVeenstra> (get-command Get-MailDomainInfo).Parameters

    Key Value
    — —–
    DomainName System.Management.Automation.ParameterMetadata
    DNSserver System.Management.Automation.ParameterMetadata
    Verbose System.Management.Automation.ParameterMetadata
    Debug System.Management.Automation.ParameterMetadata
    ErrorAction System.Management.Automation.ParameterMetadata
    WarningAction System.Management.Automation.ParameterMetadata
    InformationAction System.Management.Automation.ParameterMetadata
    ProgressAction System.Management.Automation.ParameterMetadata
    ErrorVariable System.Management.Automation.ParameterMetadata
    WarningVariable System.Management.Automation.ParameterMetadata
    InformationVariable System.Management.Automation.ParameterMetadata
    OutVariable System.Management.Automation.ParameterMetadata
    OutBuffer System.Management.Automation.ParameterMetadata
    PipelineVariable System.Management.Automation.ParameterMetadata

    My Get-MailDomainInfo Function does not have cmdletbinding 🙂

    function Get-MailDomainInfo {
    param(
    [parameter(Mandatory = $true)][string[]]$DomainName,
    [parameter(Mandatory = $false)][string]$DNSserver = ‘1.1.1.1’
    )
    …..

Leave a Reply

Your email address will not be published. Required fields are marked *