Mandatory Parameters in PowerShell (With Examples)

To make a parameter mandatory in PowerShell, put [Parameter(Mandatory)] right above it in the param block. If the caller leaves it out, PowerShell stops and asks for the value:

param(
    [Parameter(Mandatory)]
    [string]$Customer
)

In this tutorial, I’ll show you how mandatory parameters in PowerShell behave: the prompt, help messages, arrays, empty strings, parameter sets and scripts that run unattended.

The sample function we’ll use

Every method builds on Send-Invoice, one of those small PowerShell functions a billing team in Austin, TX might use. It needs a customer name, and the amount is optional:

function Send-Invoice {
    param(
        [Parameter(Mandatory)]
        [string]$Customer,

        [decimal]$Amount = 0
    )
    "Invoice for `$$Amount sent to $Customer"
}

Send-Invoice -Customer 'Maria Lopez' -Amount 1250.75

Output:

Invoice for $1250.75 sent to Maria Lopez

Method 1: Make a parameter mandatory with [Parameter(Mandatory)]

The shorthand Mandatory and the older Mandatory = $true mean exactly the same thing. Here’s the long form, plus a check with Get-Help:

function Send-Reminder {
    param(
        [Parameter(Mandatory = $true)]
        [string]$Customer
    )
    "Reminder sent to $Customer"
}

Send-Reminder -Customer 'Tom Baker'
"Required: $((Get-Help Send-Reminder -Parameter Customer).required)"

Output:

Reminder sent to Tom Baker
Required: true

How does this code work? The Parameter attribute’s Boolean arguments default to False, so naming Mandatory alone switches it on. Get-Help reads that flag and reports the parameter as required.

Both forms ran the same in PowerShell 7 and Windows PowerShell 5.1, so pick the shorter one. You’ll still see Mandatory = $true in older scripts and in Microsoft’s own examples.

What happens when you leave out a mandatory parameter?

PowerShell pauses and asks for each missing value, in the order they’re declared. This version adds a HelpMessage to Customer and makes Amount mandatory too:

function Send-Invoice {
    param(
        [Parameter(Mandatory, HelpMessage = 'Customer full name, like Maria Lopez')]
        [string]$Customer,

        [Parameter(Mandatory)]
        [decimal]$Amount
    )
    "Invoice for `$$Amount sent to $Customer"
}

Send-Invoice

I ran it and typed !? at the first prompt, then the two values:

cmdlet Send-Invoice at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Customer: !?
Customer full name, like Maria Lopez
Customer: Maria Lopez
Amount: 1250.50
Invoice for $1250.5 sent to Maria Lopez
PowerShell 7 prompting for a missing mandatory parameter and showing the HelpMessage after !?
Typing !? shows the HelpMessage, then PowerShell asks for Customer again (PowerShell 7)

How does this code work? The “(Type !? for Help.)” hint only appears when a parameter has a HelpMessage. Typing !? prints the message and repeats the same prompt.

Notice the amount. Typed values arrive as text and get converted to [decimal]. PowerShell 7 printed $1250.5, while Windows PowerShell 5.1 kept the zero and printed $1250.50, so format money with '{0:N2}' -f $Amount.

When I typed abc into an [int] prompt, both versions printed “Cannot recognize “abc” as a System.Int32 due to a format error.” and asked again.

Method 2: Make an array parameter mandatory

A mandatory [string[]] parameter prompts for one item at a time, with an index after the name. Press Enter on an empty line to finish:

function Add-Store {
    param(
        [Parameter(Mandatory)]
        [string[]]$City
    )
    "Added $($City.Count) stores: $($City -join ', ')"
}

Add-Store
cmdlet Add-Store at command pipeline position 1
Supply values for the following parameters:
City[0]: Austin
City[1]: Denver
City[2]: Seattle
City[3]:
Added 3 stores: Austin, Denver, Seattle
Windows PowerShell 5.1 prompting City[0], City[1] and City[2] for a mandatory array parameter
A mandatory string array prompts item by item until you press Enter on an empty line (Windows PowerShell 5.1)

Both versions behaved the same. Passing -City @() fails with “because it is an empty array”, unless you add [AllowEmptyCollection()]. My post on passing arrays to PowerShell parameters covers the calling side.

Why does a mandatory parameter reject an empty string?

Mandatory means “has a real value”. Here I pass an empty string, $null and three spaces:

function Send-Invoice {
    param([Parameter(Mandatory)][string]$Customer)
    "Invoice sent to [$Customer]"
}

foreach ($value in '', $null, '   ') {
    try { Send-Invoice -Customer $value }
    catch { "Failed. $($_.Exception.Message)" }
}

Output:

Failed. Cannot bind argument to parameter 'Customer' because it is an empty string.
Failed. Cannot bind argument to parameter 'Customer' because it is an empty string.
Invoice sent to [   ]