Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make parameters mandatory in PowerShell?

Tags:

powershell

How do I make parameters mandatory in PowerShell?

like image 268
Alex58 Avatar asked Aug 23 '11 11:08

Alex58


People also ask

How do I set a default parameter 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.

Are parameters Mandatory?

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.

How do you pass parameters in PowerShell?

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 " .

What is param () in PowerShell?

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.


3 Answers

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{
       ...
    }
}
like image 134
Jamiec Avatar answered Oct 16 '22 22:10

Jamiec


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?
like image 23
llmora Avatar answered Oct 16 '22 23:10

llmora


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!)

like image 12
uryga Avatar answered Oct 17 '22 00:10

uryga