The Conditional If does not produce error if specific syntax is incorrect
See original GitHub issueSteps to reproduce:
Using the conditional If, use the incorrect comparison operator of = instead of -eq:
$Sky = 'blue'
if ($Sky = 'green') {
Write-Host "The sky is green"
}
Expected behavior
We should get the error Unexpected token '=' in expression or statement like this if we used the incorrect comparison operator equals:
ParserError:
Line |
3 | if ($Sky equals 'green') {
| ~~~~~~
| Unexpected token 'equals' in expression or statement.
The variable should also not have been changed to green as you don’t set variables inside of the condition to be met inside of conditionals.
Actual behavior
No error is produced, the script continues, and the variable is set to green
## Environment data
Observed in both PowerShell & Windows PowerShell
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
Makefile conditional error
The shell doesn't understand ifeq ; that's a make construct. You'll have to use shell if-statements inside a recipe. And, you don't have...
Read more >if / else errors - learn how to fix these
Have a look and make sure you don't have any semicolons on your `else` or ... Typically the following error messages are caused...
Read more >Why is there a syntax error if I don't write 'if' in an END block ...
AWK programs are a series of rules, and possibly functions. ... be specified as part of an if statement (or other conditional statement, ......
Read more >Using IF with AND, OR and NOT functions
The IF function allows you to make a logical comparison between a value and what you expect by testing for a condition and...
Read more >Conditionals with if/else & Booleans | AP CSP (article)
The condition is a Boolean expression: an expression that evaluates to either true or false . Boolean values are another type of data...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This is by design.
What’s happening here is that the expression
$sky = 'green', which doesn’t normally output data, can also be written($sky = 'green'), which does produce output (it outputs the value'green'to the pipeline as well as storing it in the variable).Knowing this, the parentheses in an
ifstatement (and indeed other conditionals likeswitch) behave the same way, forcing the output and processing it in the conditional statement.C# works the same way, it’s likely a pattern copied from there.
In other words, assigning a variable in an if statement will always be an assignment and a test of the value you assigned, and never a test of the current value of the variable.
Thank you @vexx32