Validating Input Type

by Dec 30, 2009

The -as parameter is not widely known but is extremely versatile. It tries to convert data into a .NET type, and when it fails, it simply returns $null. This way, you can create your own functions to test data types. For example, you should use the following Test-Numeric function to validate whether someone has entered a numeric value:

function Test-Numeric($test) {
($test -as [Double]) -ne $null
}

Twitter This Tip! ReTweet this Tip!