Negation

suggest change

You 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'"
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents