Convert to Numeric

by Jan 18, 2012

Whenever PowerShell asks for user input or reads text file content, the results are text strings. If you expect numbers and want to calculate, make sure you cast them to a numeric format. Have a look:

PS> $number = Read-Host 'Enter a number'
Enter a number: 100
PS> $number * 12

Now try the same with casting:

PS> $number = [Double](Read-Host 'Enter a number')
Enter a number: 100
PS> $number * 12

Twitter This Tip!
ReTweet this Tip!