about_Scripting_ValidatePattern.help.txt

TOPIC
    about_Scripting_ValidatePattern
 
SHORT DESCRIPTION
    A tutorial on validating a parameter value based on a regular expression
    pattern match.
     
LONG DESCRIPTION
    This topic will cover using a regular expression pattern to validate a
    parameter value. I’m going to assume you have a rudimentary knowledge of
    how to use regular expressions in PowerShell. If not, there is an entire
    chapter devoted to the topic in Windows PowerShell 2.0: TFM by Don Jones
    and Jeffery Hicks (SAPIEN Press).
 
    The parameter attribute is [ValidatePattern()]. Inside the parentheses
    you place a scriptblock with the regular expression pattern. For example,
    in PowerShell we might write a command like this to verify if something
    is a number of 1 to 3 digits.:
 
    $x -match "^\d{1,3}$"
     
    To use that pattern in a [ValidatePattern()] attribute, you would write
    it like this:
 
    [ValidatePattern({^\d{1,3}$})]
     
    There is no need to use the -match operator or $_. Sure, I suppose you
    could write a validation script to achieve the same effect, but this is
    just as easy.
     
    I recommend testing your pattern from the PowerShell prompt, especially
    testing for failures. Here’s a more complete example.
 
    Param (
    [Parameter(Position=0,Mandatory=$True,
    HelpMessage="Enter a UNC path like \\server\share")]
    [ValidatePattern({^\\\\\S*\\\S*$})]
    [ValidateScript({Test-Path -Path $_ })]
    [string]$Path
    )
 
    Write-Host "Getting top level folder size for $Path" -ForegroundColor Red
     
    dir $path | measure-object -Property Length -sum
     
    For you regular expression gurus, don’t get hung up on my pattern. It
    works for my illustration purposes. Your pattern can be as simple or as
    complex as you need it to be.
     
    In this short script I’m expecting a path value like \\file01\public. If
    the value is not in this format, the pattern validation will fail,
    PowerShell will throw an exception and the script will fail.
 
    Notice I’m also using a second parameter validation attribute,
    [ValidateScript()]. It is possible for the pattern to be correct but
    invalid so I can combine both validation tests.
 
    PS C:\Scripts\> .\Demo-ValidatePattern.ps1 '\\file01\temp'
    C:\scripts\Demo-ValidatePattern.ps1 : Cannot validate argument on para
    meter 'Path'. The "Test-Path -Path $_ " validation script for the argu
    ment with value "\\file01\temp" did not return true. Determine why the
    validation script failed and then try the command again.
    At line:1 char:28
    + C:\Scripts\Demo-ValidatePattern.ps1 <<<< '\\file01\temp'
        + CategoryInfo : InvalidData: (:) [Demo-ValidatePattern.ps
        1], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Demo-Vali
        datePattern.ps1
     
    The bottom line is that any regular expression pattern you can use with
    the -match operator should work with this validation attribute.
 
SEE ALSO
    about_Functions_Advanced_Parameters
    about_Regular_Expressions
    about_Comparison_Operators
    http://jdhitsolutions.com/blog