Mandatory Parameters¶
Mandatory parameters are parameters that must be supplied when calling a function. If the caller does not provide a value, PowerShell automatically prompts for it. Mandatory parameters are essential when a function cannot run correctly without specific input.
Mandatory parameters are defined using the [Parameter(Mandatory = $true)] attribute inside the param() block.
1. Basic Syntax for Mandatory Parameters¶
The general structure is:
function Name {
param(
[Parameter(Mandatory = $true)]
$ParameterName
)
<code>
}
- The
[Parameter()]attribute modifies how the parameter behaves. Mandatory = $truetells PowerShell that the parameter is required.
2. Example: A Function With One Mandatory Parameter¶
function Show-Message {
param(
[Parameter(Mandatory = $true)]
$Text
)
"Message: $Text"
}
Show-Message
Explanation:
- The function requires a value for
$Text. - Calling
Show-Messagewithout arguments triggers an interactive prompt:
cmdlet Show-Message at command pipeline position 1
Supply values for the following parameters:
Text:
The user must enter a value before the function runs.
3. Mandatory Parameters With Named Arguments¶
You can call the function using the parameter name:
Show-Message -Text "Hello"
This runs without prompting because the required parameter is supplied.
4. Multiple Mandatory Parameters¶
You can mark more than one parameter as mandatory.
function Add-Numbers {
param(
[Parameter(Mandatory = $true)]
[int]$A,
[Parameter(Mandatory = $true)]
[int]$B
)
$A + $B
}
Calling without both values:
Add-Numbers
PowerShell prompts for both $A and $B.
Calling with values:
Add-Numbers -A 5 -B 7
Returns:
12
5. Mandatory Parameters With Type Conversion¶
Mandatory parameters still support type conversion:
function Multiply {
param(
[Parameter(Mandatory = $true)]
[int]$X,
[Parameter(Mandatory = $true)]
[int]$Y
)
$X * $Y
}
Multiply -X "3" -Y "4"
Explanation:
"3"and"4"are strings.- PowerShell converts them to integers.
- The function returns
12.
If conversion fails, PowerShell stops and reports an error.
6. Mandatory Parameters With Validation¶
Mandatory parameters can also use validation attributes.
function Set-Age {
param(
[Parameter(Mandatory = $true)]
[ValidateRange(1,120)]
[int]$Age
)
"Age set to $Age"
}
Calling:
Set-Age
PowerShell prompts for $Age.
If the user enters 200, validation fails and PowerShell displays an error.
7. Mandatory Parameters and Pipeline Input¶
Mandatory parameters can also accept pipeline input.
If the pipeline does not supply a value, PowerShell prompts for it.
function Show-Item {
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
$Item
)
"Item: $Item"
}
"apple","banana" | Show-Item
Explanation:
- Each pipeline value is bound to
$Item. - No prompting occurs because the pipeline supplies the values.
8. Why Mandatory Parameters Matter¶
Mandatory parameters ensure:
- The function always receives the data it needs
- Errors occur early, not halfway through execution
- Scripts behave predictably
- Users cannot accidentally run a function incorrectly
They are essential for building reliable, modular PowerShell code.