Negation
suggest changeYou may want to negate a boolean value, that is, enter an if statement when a condition is false rather than true. This can be done with the -not operator. -not has higher precedence than -eq, so parenthesize the comparison:
$test = "test"
if (-not ($test -eq "test2")){
Write-Host "if condition not met"
}
You can also use !:
$test = "test"
if (!($test -eq "test2")){
Write-Host "if condition not met"
}
There is also the -ne (not equal) operator:
$test = "test"
if ($test -ne "test2"){
Write-Host "variable test is not equal to 'test2'"
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents