Limiting String Input Length

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!


Posted Aug 23 2010, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.