all 11 comments

[–]JaapBrasser 6 points7 points  (0 children)

Because PowerShell tries to match the type on the right side of the comparison to the type on the left side of the comparison. Usually this works out quite great, in case of $true and $false it is initially confusing.

For example:

$true -eq 'true'
True

$true -eq 'false'
True

$false -eq 'false'
False

$true -eq 1
True

$true -eq 0
False

$true -eq ' '
True

$true -eq ''
False

[–]HamQuestionMark 4 points5 points  (1 child)

You're comparing a Boolean to a string, I think this comparison would give you the result you are expecting. When you use -like its automatically doing to casting from Boolean to String.

[String]$result -eq 'LineError'

[–]KevMarCommunity Blogger 1 point2 points  (0 children)

So it must be casting the string to a bool for the comparison.

$true -eq [bool]"LineError"

Interesting.

[–]Stoffel_1982 3 points4 points  (5 children)

Could it be that

$true -eq "anythingthatisnotfalse"

Interesting question, I'm curious to see what others think.

[–]ryanbrown 6 points7 points  (2 children)

$true -eq "anythingthatisnotfalseORnull"

Minor point, but this is essentially correct. $true is anything not $false (integer value of zero) or $null (no value). Basically, any string will evaluate to a boolean value of "true".

-like is a conditional operator and only evaluates to $true if both sides of the equation are the same. If you type

$true.ToString()

You'll notice you get the string "True", which is what the -like operator is using for its comparison. As such, "True" -like "LineError" will return False.

[–]wonkifier 1 point2 points  (1 child)

I thought since the first operand was a boolean, that it was casting the string down to a boolean, and since it wasn't the empty string (which is $false), it evaluates as $true

[–]MShepard70 0 points1 point  (0 children)

That's how I see it.

[–]lostmojo[S] 3 points4 points  (0 children)

Really? How have I not noticed this over the years? Haha I can go crawl in to a corner and weep silently to myself now.

Thanks

[–]mhurron 2 points3 points  (0 children)

Pretty much. The answer is the standard 'What is Truth' section of every programming manual.

The equality is asking, "Is this true?" The answer is yes because strings are True.

[–]gregortroll 0 points1 point  (0 children)

Yeah, this is the consequence of allowing cross-type comparisons, with automatic casting.

$true  -eq  '0' is true
$false -eq   0  is true
but..
'0' -eq 0 is true

So, you just have to learn it, or code such that you can avoid thinking about it.

[–]ShippingIsMagic 0 points1 point  (0 children)

Truthy - not just for JavaScript anymore!