PowerShell Conditional logic Negation


Example

You may want to negate a boolean value, i.e. enter an if statement when a condition is false rather than true. This can be done by using the -Not CmdLet

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