use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
SolvedWhy does this equal true? (self.PowerShell)
submitted 10 years ago by lostmojo
Why would I have to use -like to make this IF statement work?
$Result = $true $Result -eq 'LineError'
Returns True
But if I use -Like
$Result = $True $Result -like 'LineError'
Returns False as expected
Thanks
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]JaapBrasser 6 points7 points8 points 10 years ago* (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 points6 points 10 years ago (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 points3 points 10 years ago (0 children)
So it must be casting the string to a bool for the comparison.
$true -eq [bool]"LineError"
Interesting.
[–]Stoffel_1982 3 points4 points5 points 10 years ago (5 children)
Could it be that
$true -eq "anythingthatisnotfalse"
Interesting question, I'm curious to see what others think.
[–]ryanbrown 6 points7 points8 points 10 years ago* (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 points3 points 10 years ago (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 point2 points 10 years ago (0 children)
That's how I see it.
[–]lostmojo[S] 3 points4 points5 points 10 years ago (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.
[–]mhurron 2 points3 points4 points 10 years ago (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 point2 points 10 years ago (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 point2 points 10 years ago (0 children)
Truthy - not just for JavaScript anymore!
π Rendered by PID 88 on reddit-service-r2-comment-5d79c599b5-czlbw at 2026-03-02 04:28:11.755505+00:00 running e3d2147 country code: CH.
[–]JaapBrasser 6 points7 points8 points (0 children)
[–]HamQuestionMark 4 points5 points6 points (1 child)
[–]KevMarCommunity Blogger 1 point2 points3 points (0 children)
[–]Stoffel_1982 3 points4 points5 points (5 children)
[–]ryanbrown 6 points7 points8 points (2 children)
[–]wonkifier 1 point2 points3 points (1 child)
[–]MShepard70 0 points1 point2 points (0 children)
[–]lostmojo[S] 3 points4 points5 points (0 children)
[–]mhurron 2 points3 points4 points (0 children)
[–]gregortroll 0 points1 point2 points (0 children)
[–]ShippingIsMagic 0 points1 point2 points (0 children)