How do I make parameters mandatory in PowerShell?
You can use a script block to specify different default values for a parameter under different conditions. PowerShell evaluates the script block and uses the result as the default parameter value. The Format-Table:AutoSize key sets that switch parameter to a default value of True.
The Mandatory Parameter Attribute PowerShell has a concept called parameter attributes and parameter validation. Parameter attributes change the behavior of the parameter in a lot of different ways. For example, one of the most common parameter attributes you'll set is the Mandatory keyword.
A default value will not work with a mandatory parameter. You can omit the =$true for advanced parameters of type boolean [Parameter(Mandatory)] . @Andrew First of all you have to change the type of the parameter to [string] . If you then want to pass a string as parameter you can use either ' or " .
The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.
You specify it in an attribute above each parameter like this:
function Do-Something{
[CmdletBinding()]
param(
[Parameter(Position=0,mandatory=$true)]
[string] $aMandatoryParam,
[Parameter(Position=1,mandatory=$true)]
[string] $anotherMandatoryParam)
process{
...
}
}
To make a parameter mandatory add a "Mandatory=$true" to the parameter description. To make a parameter optional just leave the "Mandatory" statement out.
This code works for both script and function parameters:
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String]$aMandatoryParameter,
[String]$nonMandatoryParameter,
[Parameter(Mandatory=$true)]
[String]$anotherMandatoryParameter
)
Make sure the "param" statement is the first one (except for comments and blank lines) in either the script or the function.
You can use the "Get-Help" cmdlet to verify the parameters have been defined correctly:
PS C:\> get-help Script.ps1 -full
[...]
PARAMETERS
-aMandatoryParameter <String>
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters?
-NonMandatoryParameter <String>
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters?
-anotherMandatoryParameter <String>
Required? true
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters?
just wanted to post another solution, since i find param(...)
blocks kind of ugly.
looks like this code:
function do-something {
param(
[parameter(position=0,mandatory=$true)]
[string] $first,
[parameter(position=1,mandatory=$true)]
[string] $second
)
...
}
can also be written more concisely like this:
function do-something (
[parameter(mandatory)] [string] $first,
[parameter(mandatory)] [string] $second
) {
...
}
which looks much nicer! the =$true
can be omitted because mandatory
is a switch parameter.
(disclaimer: i'm pretty new to PS, and it's possible that this solution has some corner case i'm not aware of. if so, please let me know!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With