Post increments an integer in PowerShell, initialized with a negative value

I have the following code that initializes the value -1:

Set-Variable -Name ID -Value -1 -Scope local 

When I do it like this

 $local:ID++ 

I get the following error:

The operator "++" only works with numbers. The operand is 'System.String'.

I thought this would mean this as an int. But I have to use a workaround that is not neat:

Workaround:

 $intVal = [int]$local:ID $intval ++ 

Is there any other approach?

+4
source share
1 answer

$ id is a string (enter $ id.gettype () to find its type). To initialize it as an integer, put it in brackets:

 PS> Set-Variable -Name ID -Value (-1) -Scope local PS> ($local:ID++) #increment and print the variable 0 
+3
source

Source: https://habr.com/ru/post/1412365/


All Articles