Powershell How to Add a Number to a Variable

how can i add a number to another number contained in a variable?

$t0 = Get-date -UFormat "%H%M" $t1 = $t0 + 10 

so if $ t0 is 1030, I would get $ t1 1040.

+4
source share
3 answers

force to [int] before assigning the value $ t0 ( get-date -uformat returns type [string]):

 [int]$t0 = Get-date -UFormat "%H%M" $t1 = $t0 + 10 

if you change the order, the powershell force start function will give the expected value:

 $t0 = Get-date -UFormat "%H%M" $t1 = 10 + $t0 

because the second operand is cast to the type of the first

+6
source

After executing $t0 = Get-date -UFormat "%H%M" $t0 does not contain a number, but a string. You can verify this by calling $t0 | Get-Member $t0 | Get-Member

One easy way around this is to pass it to int: [int]$t0 + 10 , which will do a normal integer addition.

+4
source

This will be done:

 $t1 = [int]$t0 + 10 
+3
source

All Articles