If you are writing functions, instead of writing custom blocks to validate your input, you can use the built in parameter validation. Here's a technet article that describes in in detail. In the ISE you can hit Ctrl+J and select advanced-function complete to auto create an example. Here's a simplified version.
function Test-ParameterValidation
{
[OutputType([String])]
Param
(
# Param1 help description
[Parameter(Mandatory=$true,
ValueFromPipeline=$true)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidateCount(1,5)]
[ValidateSet("sun", "moon", "earth")]
[Alias("p1")]
$Param1,
[AllowNull()]
[AllowEmptyCollection()]
[AllowEmptyString()]
[ValidateScript({$true})]
[ValidateRange(0,5)]
[int]
$Param2,
[ValidatePattern("^\d{5}(?:[-\s]\d{4})?$")]
[ValidateLength(5,10)]
[String]
$Param3
)
process{
"p1 is $Param1"
"p2 is $Param2"
"p3 is $Param3"
}
}
And sample inputs/outputs.
C:\ > Test-ParameterValidation -Param1 earth,moon
p1 is earth moon
p2 is 0
p3 is
C:\ > Test-ParameterValidation -Param1 earth,moon,moon,moon,moon,moon
Test-ParameterValidation : Cannot validate argument on parameter 'Param1'. The number of supplied arguments (6) exceeds the maximum number of allowed arguments (5). Specify less than 5 arguments and then try the command again.
At line:1 char:34
+ Test-ParameterValidation -Param1 earth,moon,moon,moon,moon,moon
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Test-ParameterValidation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-ParameterValidation
C:\ > Test-ParameterValidation -Param1 earth,moon,moon,moon -Param2 5 -Param3 ABC
Test-ParameterValidation : Cannot validate argument on parameter 'Param3'. The number of characters (3) in the argument is too small. Specify an argument whose length is greater than or equal to "5" and then try the command again.
At line:1 char:73
+ Test-ParameterValidation -Param1 earth,moon,moon,moon -Param2 5 -Param3 ABC
+ ~~~
+ CategoryInfo : InvalidData: (:) [Test-ParameterValidation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-ParameterValidation
C:\ > Test-ParameterValidation -Param1 earth,moon,moon,moon -Param2 5 -Param3 ABCDEF
Test-ParameterValidation : Cannot validate argument on parameter 'Param3'. The argument "ABCDEF" does not match the "^\d{5}(?:[-\s]\d{4})?$" pattern. Supply an argument that matches "^\d{5}(?:[-\s]\d{4})?$" and try the command again.
At line:1 char:73
+ Test-ParameterValidation -Param1 earth,moon,moon,moon -Param2 5 -Param3 ABCDEF
+ ~~~~~~
+ CategoryInfo : InvalidData: (:) [Test-ParameterValidation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-ParameterValidation
C:\ > Test-ParameterValidation -Param1 earth,moon,moon,moon -Param2 5 -Param3 11111-1111
p1 is earth moon moon moon
p2 is 5
p3 is 11111-1111
C:\ > Test-ParameterValidation -Param1 earth,moon,moon,moon -Param2 6
Test-ParameterValidation : Cannot validate argument on parameter 'Param2'. The 6 argument is greater than the maximum allowed range of 5. Supply an argument that is less than or equal to 5 and then try the command again.
At line:1 char:63
+ Test-ParameterValidation -Param1 earth,moon,moon,moon -Param2 6
+ ~
+ CategoryInfo : InvalidData: (:) [Test-ParameterValidation], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-ParameterValidation
These give you the ability to present validation errors in a standard "powershelly" way without having to write anything extra. Although you can use the ValidateScript() attribute if you need some heavy duty validation.