Posey's Tips & Tricks
How To Validate Input in PowerShell Functions, Part 2
As Brien demonstrates, string validation is not the only type of input validation that PowerShell can perform.
In Part 1 of this series, I showed you an example of PowerShell's native validation capabilities.
In that installment, I wrote a function that accepted a text string as input. The function then used [ValidateSet] to check whether the text string matched any of the acceptable values.
As a reminder, here is what the script looked like:
write-host "The Salsa is " $Spice
Salsa -Spice Mild
Write-Host "The script is finished".
As helpful as this type of validation can be, string validation is not the only type of input validation that PowerShell can perform. It is just as easy to do numerical validation. To perform a numerical validation, you would replace ValidateSet with ValidateRange. The text strings (Medium, Hot and Extra Hot, in this case), would need to be replaced by the acceptable numerical range.
Let's suppose, for example, that you wanted to modify the above function to check if the input is greater than zero but less than 11. To do so, you would replace the ValidateSet line with this one:
This line of code tells PowerShell that the acceptable values can be 1, 2, 3, 4, 5, 6, 7, 8, 9 or 10. Incidentally, you do have to make one more change to the Param section of the function in order to get the range validation to work properly.
If you look at the script above, you can see that just below the ValidateSet line, there is a line that says [String]. We can't do this type of range validation on string data. Hence, we need to change [String] to [Int] as a way of telling PowerShell that the parameter that is being passed to the function is an integer value.
So to put all of this into context, let's rewrite the script. Originally, the script made it so that Medium, Hot and Extra Hot salsa was acceptable, but mild salsa was not. Let's instead make it so that on a scale from one to 10, heat levels of six and above are acceptable. Here is what such a script would look like:
write-host "The Salsa is " $Spice
Salsa -Spice 5
The important thing to take away from this is that the basic script essentially remained the same. The only real changes were to the data type and the validation type. With that in mind, there are some other types of validation that you can perform.
Let's suppose for a moment that you wrote a PowerShell script to create new Active Directory user accounts. Such a script might read values from a .CSV file, and one of the values that would probably be in the file is the user name. Of course, it would probably be a good idea to make sure that the user name isn't anything too short (because that might be a security risk) or too long (because that would be a pain for the user to type).
Ultimately, you decide that user names should be at least four characters but no more than eight characters. You can validate this requirement by using ValidateLength. Here is what the validation line would look like:
Incidentally, using [String] input with ValidateLength is fine.
PowerShell also allows you to validate that a string or numerical input matches a certain pattern. Now, I have to tell you upfront that a lot of people stay away from pattern validation because the code can be really messy and archaic. Even so, I wanted to give you a really simple example.
Let's suppose that I am writing a function that performs some kind of action based on zip codes, and I want to make sure that the zip code is within the state of South Carolina. South Carolina zip codes are five digits in length, and the first two characters are 2 and 9. So let's perform a pattern validation based on that. Here are the two lines that really matter:
The ValidatePattern line tells PowerShell that the input must be five characters in length, the first two characters have to be 2 and 9, and the last three characters have to be numeric. Notice that we are treating the zip code as a string in this case (hence the quotation marks in the ValidatePattern line). If I were to do the same thing with an integer input, I would instead use a range and tell PowerShell that numbers ranging from 29000 to 29999 were acceptable.
This is just a sampling of the types of validation that you can perform on PowerShell inputs. Microsoft describes some other validation techniques here.
Brien Posey is a 19-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.