why does this not evaluate to 6

$x = 5
$y = $x++
write-host $y

why is $y evaluating to 5 and not 6?

$x++ doesn’t return anything, instead it increments variable x’s value by 1. If you wan’t it to return, just wrap it in parenthesis.

$y = ($x++)

I did try that also and it is still evaluates to 5

 

It should work, it is called Variable Squeezing in PowerShell.

And as kvprasoon mentioned, $x++ will not return any value, it is used for self increment, so it can’t be assigned to $y.

Either you can use $y = $x + 1 or $y = ($x++)

Thank you.

Thank you for your help

I suspect I am doing something wrong or missing something obvious. I opened up a new shell and type the following:

PS C:\Users\superuser> $x = 5
PS C:\Users\superuser> $x
5
PS C:\Users\superuser> $y = ($x++)
PS C:\Users\superuser> $y
5

 

Okay, what is your windows? is it Server edition or Client Edition?

I think variable squeezing won’t work in Server side PowerShell.

@Kiran does it really work for you on your systems? I checked it on my client W10 1909, Windows Powershell 5.1 or Powershell 7.0.0-rc.3. The result is the same on both versions.

PS C:\> $x = 5
PS C:\> $x
5
PS C:\> $y = ($x++)
PS C:\> $y
5

[Edit]

Yes, I checked it on my Windows 10 1909 (itself) and it is working on Windows PowerShell 5.1, PowerShell Core 6.2.3 and PowerShell 7 rc3 as well and not on 6.2.3 & 7.0.0-rc3 (sorry for the little confusion)

But not working on Window Server 2019 with Windows PowerShell 5.1

 

Mine is Windows 10 pro 1903, running powershell 5.1.

[quote quote=205482]Yes, I checked it on my Windows 10 1909 (itself) and it is working on Windows PowerShell 5.1, PowerShell Core 6.2.3 and PowerShell 7 rc3 as well

But not working on Window Server 2019 with Windows PowerShell 5.1

[/quote]
interesting. Thanks

I have double checked and noticed the below…

Win PS 5.1 PS Core 6.2.3 PS 7.0.0-rc3
Windows Native Console Windows Terminal (Preview) Native Console Windows Terminal (Preview) Native Console Windows Terminal (Preview)
Windows 10 1909 YES NO NO NO NO NO
Windows Server 2019 NO
It looks very weird behavior.

++ after the variable increments after sending the value, ++ before the variable increments before sending the value.

$x = 5
$y = ++$x
$y

6

There’s a similar thing in bash (or C):

x=5
y=$((++x))
echo $y

6

… OK but I would expect the same behaviour on all systems and all versions. Wouldn’t you? :wink:

I don’t know what you mean. It works the way I said, in ps 5 and 7, with or without parentheses.

OK, but obviously it does not work as expected for all systems. Kiran confirmed it and it does not work on my system.

Kirin is mistaken.

Possible. :wink: @Kiran Did you reset your console on the system before you tried to confirm it?

This is completely make sense.

LOL. It is so weird!! Still working on my personal computer but not else where, very strange behavior.

For what it is worth, I was going through basic code shown in the book, “Windows’ PowerShell Programming for the absolute beginner” third edition, when I found this abnormality, They were using powershell 4 in the book.