Reputation: 173
PowerShell logic question:
$a = 1
$b = 2
if ($a = $b) {
$ans = $true
}
else {
$ans = $false
}
Write-Host $ans
Output:
True
Can someone explain to me why this evaluates as true? Is it because I assign $ans to true first? Also, could someone show me how to get this to evaluate the way I think it should?
Upvotes: 7
Views: 54210
Reputation: 115037
You're doing an assignment $a = $b
, the assignment succeeds and that returns true because b was true, so the first case will always evaluate to true at the moment, instead use a comparison: $a -eq $b
.
More detailed information on comparisons in powershell.
Upvotes: 10