Limiting String Input Length

by Aug 23, 2010

If a function parameter should receive a string of a given length only, you should use the following validation attribute. In the example, it limits filenames to eight characters:

function Get-FileName {
param(
[ValidateLength(1,8)]
[String]
$FileName
)

"Your filename {0} is {1} chars long" -f $FileName, $FileName.Length
}

Twitter This Tip! ReTweet this Tip!