Easy validating parameter input using ValidateCount, ValidateLength, ValidatePattern, ValidateRange, ValidateSet and ValidateScript in PowerShell

Windows PowerShell introduced parameter validation which drastically reduces lines of validation code – and replaces it with a couple of markup tags:

 

ValidateCount (int minLength, int maxlength)
How many arguments are allowed for a parameter.

ValidateLength (int minLength, int maxlength)

Length (in characters) of a parameter argument.

ValidatePattern (string regexString, Named Parameters)
Valid patterns for a parameter argument.

ValidateRange (object minRange, object maxRange)
Valid range for a parameter argument.

ValidateSet (string[] validValues, Named Parameters)
Possible values for a parameter argument.

ValidateScript (scriptblock validValues)
Custom script for validation

ValidateNotNull ()
Input is not null

ValidateNotNullOrEmpty ()
Input is neither null or empty

 

Read more about validating parameters on MSDN:

Or use PowerShell built-in help: