Validate Input Using Regular Expressions

by Aug 24, 2010

Function parameters can be validated using standard regular expressions. For example, the next template function accepts only valid Knowledge Base article numbers beginning with "KB" and a six- digit number:

function Get-KnowledgeBaseArticle {
param(
[ValidatePattern('^KB\d{6}$')]
[String]
$KB
)

"You entered Knowledgebase ID $KB"
}

Twitter This Tip! ReTweet this Tip!