PowerShell Not Operator [With Examples]

Do you want to know about the not operator in PowerShell? In this tutorial, I will explain everything about the PowerShell Not operator, its syntax, and a few examples of the not operator in PowerShell.

What is the Not Operator in PowerShell?

The Not operator in PowerShell is used to reverse the logical state of its operand. In other words, it changes True to False and False to True. This operator is represented by -not in PowerShell, but it also has an alias ! that can be used interchangeably. The Not operator in PowerShell is used to reverse the Boolean value of an expression.

Syntax

The Not operator can be used in two forms in PowerShell:

  • -not: The standard form.
  • !: The alias, which is more concise and often used in scripts for brevity.

The syntax of the Not operator in PowerShell is:

-not <expression>

or

!<expression>

PowerShell Not Operator Examples

Now, let me show you a few examples of how to use the -not operator in PowerShell.

1. Basic Example

Let’s check a basic example of how to use the Not operator in PowerShell:

$trueValue = $true
$falseValue = -not $trueValue
Write-Output $falseValue

In this example, $trueValue is set to True. By applying the -not operator, $falseValue becomes False.

I executed the above script using VS code, and you can see the output in the screenshot below:

PowerShell Not Operator

2. Using Not in Conditional Statements

The Not operator is often used in PowerShell if statements to execute code based on the negation of a condition.

Here is an example.

$number = 10

if (-not ($number -eq 5)) {
    Write-Output "The number is not 5"
}

Here, the condition checks if $number is not equal to 5. Since $number is 10, the output will be “The number is not 5”.

I executed the above PowerShell script, and you can see the output in the screenshot below:

powershell not operator in if statement

3. Combine with Other Logical Operators

The Not operator can be combined with other logical operators to form more complex conditions in PowerShell.

Here is an example.

$isRaining = $false
$isWeekend = $true

if (-not $isRaining -and $isWeekend) {
    Write-Output "You can go for a walk"
}

In this example, the message “You can go for a walk” is displayed only if it is not raining and it is the weekend.

Here is another example:

The Not operator can be combined with other logical operators like -and, and -or:

$x = 10
$y = 20
if (-not ($x -gt 15 -and $y -lt 25)) {
    Write-Host "At least one condition is false"
}

You can see the output in the screenshot below:

not operator in if statement in PowerShell

This is an example of how to use the not operator in if statement in PowerShell.

Check out PowerShell Logical Operators

4. Using the ! Alias

In PowerShell, the ! symbol can be used as an alternative to -not, but only in the context of a boolean expression.

Here is an example.

$isAdmin = $false
if (!$isAdmin) {
    Write-Host "User is not an admin"
}

5. Check for Null or Empty Strings

The Not operator can be particularly useful for checking if a string is null or empty in PowerShell.

Here is an example.

$string = ""

if (-not [string]::IsNullOrEmpty($string)) {
    Write-Output "The string is not empty"
} else {
    Write-Output "The string is empty"
}

In this case, since $string is empty, the output will be “The string is empty”.

Conclusion

The Not operator in PowerShell is used to negate Boolean expressions. In this tutorial, I explained how to use the Not operator in PowerShell and its syntax, and we also saw a few examples of the not operators in PowerShell.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.