ValidatePattern Attribute Declaration


The ValidatePattern attribute specifies a regular expression pattern that validates the argument of a cmdlet parameter. This attribute can also be used by Windows PowerShell functions.

When ValidatePattern is invoked within a cmdlet, the Windows PowerShell runtime converts the argument of the cmdlet parameter to a string and then compares that string to the pattern supplied by the ValidatePattern attribute. The cmdlet is run only if the converted string representation of the argument and the supplied pattern match. If they do not match, an error is thrown by the Windows PowerShell runtime.

[ValidatePattern(string regexString)]
[ValidatePattern(string regexString, Named Parameters)]

RegexString (String)

Required. Specifies a regular expression that validates the parameter argument.

Options (RegexOptions)

Optional named parameter. Specifies a bitwise combination of RegexOptions flags that specify regular expression options.

  • This attribute can be used only once per parameter.

  • You can use the Option parameter of the attribute to further define the pattern. For example, you can make the pattern case sensitive.

  • If this attribute is applied to a collection, each element in the collection must match the pattern.

  • The ValidatePattern attribute is defined by the ValidatePatternAttribute class.