shell – Adding up numbers in PowerShell

shell – Adding up numbers in PowerShell

PS H:> $value1 = 10
PS H:> $value2 = 10
PS H:> $value1 + $value2
20

If your numbers are stored as strings then youll need to cast them to integers like this:

PS H:> $value1 = 10
PS H:> $value2 = 20
PS H:> [int]$value1 + [int]$value2
30
$val1 = 1
$val2 = 2
$result = $val1 + $val2
$result

If you are getting input from the user, then you would need to explicitly coerce the numbers to be numbers:

[int]$val1 = Read-Host Enter value 1
[int]$val2 = Read-Host Enter value 2
$result = $val1 + $val2
$result

shell – Adding up numbers in PowerShell

Leave a Reply

Your email address will not be published.