Categories

Validating parameters

PowerShell provides a number of ways of validating the values input to the parameters in your scripts. One such way is Validate script. I came across an interesting question where the inquirer wanted to test an input parameter and throw an error if the 4th character was a “T”

One solution is:

function ptest {
[CmdletBinding()]
param(
[ValidateScript({$_.Substring(3,1) -ne "T" })]
[string]$server
)
Write-Host "$server"
}

Alternatively use a regular expression

Leave a Reply