Limiting Number of Arguments

by Aug 20, 2010

Function parameters can receive multiple values when they accept arrays. You should use this template to limit the number of values acceptable:

function Add-Users {
param(
[ValidateCount(1,3)]
[String[]]
$UserName
)

$UserName ForEach-Object { "Adding $_" }

}

Here, Add-Users will accept one to three user names as a comma-separated list. If you specify more or less, you will get an error message.

Twitter This Tip! ReTweet this Tip!