Validating Function Parameters

by Aug 31, 2011

You can use Regular Expression patterns to validate function parameters:

function Get-ZIPCode {
    param(
          [ValidatePattern('^\d{5}$')]
          [String]
          $ZIP
    )
    "Here is the ZIP code you entered: $ZIP"
}

You can add a [ValidatePattern()] attribute to the parameter that you want to validate, and specify the RegEx pattern that describes valid arguments.

Twitter This Tip!
ReTweet this Tip!