IsNumeric


Definition: Returns a Boolean value indicating whether an expression can be evaluated as a number.

 

OK, so this one turned out to be a bit more complicated than we anticipated; that’s because neither Windows PowerShell nor the .NET Framework include a “generic” method for testing whether or not a given value is a number. After toying with the notion of doing this using regular expressionswe finally decided to use Visual Basic .NET instead. In the following set of commands we assign the value 44.5 to the variable $a, then load the Microsoft.VisualBasic assembly. With the assembly loaded we can then use the Visual Basic IsNumeric function to determine whether or not 44.5 is a number ::

 

$a = 44.5
[ reflection.assembly]: : LoadWithPartialName("' Microsoft.VisualBasic")
$b = [ Microsoft.VisualBasic.Information]:: isnumeric($a)

 

As you might expect, when we run these commands and then echo back the value of $b we get the following:

 

True

 

If we change the value of $ a to“44.5a” and re-run the commands we’ll get back this:

 

False