Why is this PowerShell logic returning 'False' instead of 'True'?

October 26, 2025 3 answers
Asked by CuriousCoder99
I've been testing some logical expressions in PowerShell, and I'm a bit confused by the results I'm getting. When I run the command `$true -or $false -or $true -and $false`, it returns "False". I would expect it to return "True" because logically, it seems to be equivalent to `$true -or $false -or ($true -and $false)`, which correctly gives "True". I've tested this in both PowerShell 5.1 and 7.3.9, and it seems like something is off. Am I misunderstanding something about how PowerShell handles these expressions, or is there a peculiarity in the language?

Answers

LogicGuru42
You nailed it: the evaluation in PowerShell is left to right for operators of equal precedence. In your statement, it evaluates as `(($true -or $false) -or $true) -and $false`, which ultimately results in `False`. If you want it to evaluate as you expected, adding parentheses to clarify the order will help.
Reply to this answer
SyntaxSorcerer
The issue you're encountering is due to PowerShell treating the `-and` and `-or` operators with equal precedence, which means it evaluates them from left to right. That's different from many other programming languages where `AND` takes precedence over `OR`. Since they're evaluated in that order, your command leads to the output being `False`. So, it's not a mistake on your part, itโ€™s just the way PowerShell works!
Reply to this answer
CodeClarityFan
Totally agree! When in doubt, use parentheses to wrap your logical expressions. It enhances readability and makes sure you're getting the intended evaluation, especially in PowerShell where the operator precedence can confuse things. Itโ€™s a good habit!
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions