Restrict Input to Numeric Ranges

Let's say you'd like to set the PowerShell console cursor size. This size must be a number between 0 and 100. The following template will validate that the user cannot specify an argument outside the allowed range:

function Set-CursorSize {
param(
[ValidateRange(1,100)]
[Int]
$Percent
)

$Host.UI.RawUI.CursorSize = $Percent
}

Twitter This Tip! ReTweet this Tip!


Posted Aug 27 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.