Krausmueller.de

Validate PowerShell parameters

When you use parameters in your PowerShell script you might want to check the values for valid data. Of course you can do these checks with some if statements. But you can also specify valid values when defining the parameters.

 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132
# definition of parameters
Param(
    [Parameter(Mandatory=$True)]
    [string]$StringParameter1,

    [Parameter(Mandatory=$True)]
    [ValidateSet("String1","String2")]
    [string]$StringParameter2,

    [Parameter(Mandatory=$True)]
    [int]$IntegerParameter1,

    [Parameter(Mandatory=$True)]
    [ValidateRange(0, 5)]
    [int]$IntegerParameter2
)

# own validation of parameters
if (-not($StringParameter1 -eq "String1" -or $StringParameter1 -eq "String2")) {
    Write-Output "Error! Valid values for StringParameter1 are ""String1"" and ""String2""."
    exit
}
if ($IntegerParameter1 -lt 0 -or $IntegerParameter1 -gt 5) {
    Write-Output "Error! Value of IntegerParameter1 must be in range from 0 to 5."
    exit
}

# output of the parameter values
Write-Output "StringParameter1: $StringParameter1"
Write-Output "StringParameter2: $StringParameter2"
Write-Output "IntegerParameter1: $IntegerParameter1"
Write-Output "IntegerParameter2: $IntegerParameter2"

In this example parameters $StringParameter1 and $IntegerParameter1 are checked with if statements (lines 18 to 24). The same rules are used to check the other parameters with ValidateSet (line 7) and ValidateRange (line 14). In both cases valid strings are “String1” and “String2” and valid integers are the numbers 0 to 5. Here you can see the error generated with an if statement

PS C:\> .\ValidatePowerShellParameter.ps1 -StringParameter1 "some other string" -StringParameter2 "String2" -IntegerParameter1 3 -IntegerParameter2 5
Error! Valid values for StringParameter1 are "String1"" and "String2".
At C:\ValidatePowerShellParameter.ps1:20 char:10
+     Throw <<<<  "Error! Valid values for StringParameter1 are ""String1"" and ""String2""."
    + CategoryInfo          : OperationStopped: (Fehler! G├╝ltige... und "String2".:String) [], RuntimeException
    + FullyQualifiedErrorId : Error! Valid values for StringParameter1 are "String1"" and "String2".

and with ValidateRange.

PS C:\> .\ValidatePowerShellParameter.ps1 -StringParameter1 "String1" -StringParameter2 "String2" -IntegerParameter1 3 -IntegerParameter2 8
C:\ValidatePowerShellParameter.ps1 : Cannot validate argument on parameter 'IntegerParameter2'. The 8 argument is greater than the maximum allowed range of 5. Supply an argument that is less than 5 and then try the command again.
At line:1 char:120
+ .\ValidatePowerShellParameter.ps1 -StringParameter1 "String1" -StringParameter2 "String2" -IntegerParameter1 3 -IntegerParameter2 <<<<  8
    + CategoryInfo          : InvalidData: (:) [ValidatePowerShellParameter.ps1], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,ValidatePowerShellParameter.ps1

There are more checks available:

  • ValidateCount
  • ValidateLength
  • ValidatePattern
  • ValidateRange
  • ValidateScript
  • ValidateSet
  • ValidateNotNull
  • ValidateNotNullOrEmpty

A short example with ValidateScript:

123456789
# definition of parameters
Param(
    [Parameter(Mandatory=$True)]
    [ValidateScript({$_ -match "St.*g"})]
    [string]$StringParameter
)

# output of the parameter values
Write-Output "StringParameter: $StringParameter"

Unfortunately with ValidateScript it is not possible to access other parameters. So if valid values for a parameter depend on the value of another parameter you have to check the values outside the parameter defintion later in the code.

Also be careful when defining a default value for a parameter. If no other value for this parameter is given when executed there is no validation and you might want to check with your own code.

123456789
# definition of parameters
Param(
    [Parameter(Mandatory=$False)]
    [ValidateRange(0,5)]
    [string]$IntegerParameter=8
)

# output of the parameter values
Write-Output "IntegerParameter: $IntegerParameter"
PS C:\> .\ValidatePowerShellParameter.ps1
IntegerParameter: 8

PS C:\> .\ValidatePowerShellParameter.ps1 -IntegerParameter 8
C:\ValidatePowerShellParameter.ps1 : Cannot validate argument on parameter 'IntegerParameter'. The 8 argument is greater than the maximum allowed range of 5. Supply an argument that is less than 5 and then try the command again.
At line:1 char:43
+ .\ValidatePowerShellParameter.ps1 -IntegerParameter <<<<  8
    + CategoryInfo          : InvalidData: (:) [ValidatePowerShellParameter.ps1], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,ValidatePowerShellParameter.ps1