Default Parameter Values in PowerShell

To set a default parameter value in PowerShell, assign it inside the param() block, like [string]$City = 'Denver'. PowerShell uses that value whenever the caller skips the parameter:

function Get-Forecast {
    param([string]$City = 'Denver', [int]$Days = 3)
    "Forecast for $City, $Days days"
}
Get-Forecast
Get-Forecast -City Austin -Days 5

Output:

Forecast for Denver, 3 days
Forecast for Austin, 5 days

In this tutorial, I’ll show you how a default parameter value in PowerShell works in functions and scripts, how to tell whether a value was passed, and how $PSDefaultParameterValues sets defaults for any command.

Set a default parameter value in a PowerShell function

The pattern is type, name, equals sign, value. The first Get-Forecast call skipped both parameters, so it used Denver and 3; the second passed Austin and 5, and those won.

A caller’s value always beats the default. The same syntax works in every kind of PowerShell function.

Use an expression or another parameter as the default

A default can be any expression, and PowerShell evaluates it each time the function runs. Here the tax depends on the amount, and the year comes from Get-Date:

function New-Invoice {
    param(
        [decimal]$Amount,
        [decimal]$Tax = $Amount * 0.08,
        [string]$Year = (Get-Date -Format 'yyyy')
    )
    "Amount: $Amount | Tax: $Tax | Year: $Year"
}
New-Invoice -Amount 250
New-Invoice -Amount 250 -Tax 0

Output:

Amount: 250 | Tax: 20.00 | Year: 2026
Amount: 250 | Tax: 0 | Year: 2026

Tax came out as 20.00 from the bound $Amount, and -Tax 0 skipped the expression.

Order matters, though. A default can only read parameters declared above it. Watch what happens when Prefix reads $Region, which is declared after it:

function Get-Label {
    param([string]$Prefix = "Store-$Region", [string]$Region = 'West')
    "Prefix: $Prefix | Region: $Region"
}
Get-Label
$Region = 'East'
Get-Label

Output:

Prefix: Store- | Region: West
Prefix: Store-East | Region: West

The first call got an empty region; the second picked up the caller’s $Region (East), not West. That’s dynamic variable scope at work, so declare dependencies first.

Can a mandatory parameter have a default value?

No. PowerShell accepts the syntax, but it ignores the default on a parameter marked [Parameter(Mandatory)]:

function Get-Office {
    param([Parameter(Mandatory)][string]$City = 'Denver')
    "Office: $City"
}
try { Get-Office -ErrorAction Stop }
catch { "Failed. $($_.Exception.Message)" }

Output:

Failed. Cannot process command because of one or more missing mandatory parameters: City.

My test ran with -NonInteractive, so it failed instead of prompting for City. Either way, Denver is never used.

Pick one: optional with a default, or mandatory without one. The missing an argument for parameter error is a related mistake worth knowing.

How do I check if a parameter was passed in PowerShell?

Use the automatic variable $PSBoundParameters. It’s a dictionary of the parameters the caller actually passed, and default values aren’t in it:

function Send-Report {
    param([string]$City = 'Denver')
    $passed = $PSBoundParameters.ContainsKey('City')
    "City: $City | Passed by caller: $passed"
}
Send-Report
Send-Report -City Denver

Output:

City: Denver | Passed by caller: False
City: Denver | Passed by caller: True
PowerShell 7 Send-Report function showing $PSBoundParameters is False for the default and True when passed
$PSBoundParameters tells a default apart from a passed value (PowerShell 7)

Both lines show Denver, but only the second was passed. Put the ContainsKey() check in an if statement when the function should only change a setting the caller asked for.

Switch parameters default to $false

A [switch] parameter needs no default. It’s False when the caller leaves it out and True when they add it:

function Remove-OldLog {
    param([switch]$Force)
    $passed = $PSBoundParameters.ContainsKey('Force')
    "Force: $Force | Passed: $passed"
}
Remove-OldLog
Remove-OldLog -Force
Remove-OldLog -Force:$false

Output:

Force: False | Passed: False
Force: True | Passed: True
Force: False | Passed: True

Look at the last line. -Force:$false sets Force to False, yet $PSBoundParameters still lists it. Base your logic on the switch’s value, not on whether it was passed.

PowerShell accepts [switch]$Compress = $true, and I confirmed it returns True by default, but callers then need -Compress:$false. Name the switch for the rare behavior instead, like -NoCompress. See the PowerShell switch parameter.

Set defaults for any command with $PSDefaultParameterValues

The $PSDefaultParameterValues preference variable sets defaults from outside a command. Each key is CommandName:ParameterName, and the value is the default. I tried it on a basic and an advanced function:

function Get-Branch {
    param([string]$City = 'Denver')
    "Basic function: $City"
}
function Get-Store {
    [CmdletBinding()]
    param([string]$City = 'Denver')
    "Advanced function: $City"
}
$PSDefaultParameterValues['Get-Branch:City'] = 'Seattle'
$PSDefaultParameterValues['Get-Store:City'] = 'Seattle'
Get-Branch
Get-Store

Output:

Basic function: Denver
Advanced function: Seattle