Booleans in PowerShell

Booleans seem like just about the simplest things. They are, after all, binary. Either $True or $False. But Windows PowerShell’s assumption that the left side of an expression should control how the right side is interpreted can sometimes confuse you. Or at least me. For example:

PS> $True –eq 2

True

PS> 2 –eq $True

False

Yup. That makes perfect sense. If you realize that in the first case, what is happening is:

[Bool]$True –eq [Bool]2

but in the second case:

[int]2 –eq [int]$True

In the first case, the number two is evaluated as a Boolean, and any non-zero value is considered true. In the second case, the integer value of 2 causes $true to be evaluated as an integer, and the integer value of $true is 1. So the two integers aren’t equal.

I put up an article on the TechNet Wiki here. Add to it, show some more examples, make it better. That’s the Wiki way. Smile

Comments are closed.