PowerShell Logical Operators [With Examples]

PowerShell logical operators allow you to perform logical operations on values and expressions. These operators, such as And, Or, and Not, allow users to connect and evaluate multiple conditions within a single statement. Logical operators in PowerShell are often used within conditional statements or loops to evaluate specific conditions.

In this tutorial, I will show you various logical operators in PowerShell and some examples of of how to use logical operators in PowerShell.

PowerShell Logical Operators

Logical operators in PowerShell are used to perform logical operations on expressions and return Boolean values (True or False). They are fundamental in controlling the flow of scripts and making decisions based on multiple conditions. PowerShell supports several logical operators like AND, OR, NOT, and XOR, etc.

Various Logical Operators in PowerShell

Now, let us discuss various logical operators in PowerShell with examples.

Logical AND Operator

The AND operator (-and) in PowerShell returns True only if both expressions being compared are True. If either of the expressions is False, the result will be False. This operator is useful when you need to ensure that multiple conditions are met before proceeding with a task.

Syntax:

Here is the syntax of PowerShell logical and operator.

<expression1> -and <expression2>

Example:

Here is an example of how to use the logical -and operator in PowerShell.

$age = 25
$hasLicense = $true

if ($age -ge 18 -and $hasLicense) {
    Write-Output "You are eligible to drive."
} else {
    Write-Output "You are not eligible to drive."
}

In this example, the script checks if the age is greater than or equal to 18 and if the person has a driving license. Only if both conditions are True, the message “You are eligible to drive.” is displayed.

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

Logical Operators in PowerShell

Check out PowerShell Arithmetic Operators

Logical OR Operator

The PowerShell logical OR operator (-or) returns True if at least one of the expressions being compared is True. If both expressions are False, the result will be False. This operator is useful when you need to execute a task if any one of multiple conditions is met.

Syntax:

Here is the syntax.

<expression1> -or <expression2>

Example:

Here is an example of how to use the logical -or operator in PowerShell.

$isWeekend = $false
$isHoliday = $true

if ($isWeekend -or $isHoliday) {
    Write-Output "You can take a day off."
} else {
    Write-Output "You need to work today."
}

In this example, the script checks if it is either a weekend or a holiday. If either condition is True, the message “You can take a day off.” is displayed.

After executing the above PowerShell, you can see the output in the screenshot below.

Logical Operators in PowerShell example

Logical NOT Operator

The NOT operator (-not) in PowerShell negates the Boolean value of an expression. If the expression is True, it returns False, and if the expression is False, it returns True. This operator is useful when you need to reverse the logical state of an expression.

Syntax:

Here is the syntax of PowerShell logical -not operator.

-not <expression>

Example:

Here is an example of how to use the logical -not operator in PowerShell.

$isRaining = $true

if (-not $isRaining) {
    Write-Output "You can go for a walk."
} else {
    Write-Output "Better stay indoors."
}

In this example, the script checks if it is not raining. If it is not raining, the message “You can go for a walk.” is displayed.

Logical XOR Operator

The XOR operator (-xor) returns True if only one of the expressions is True. If both expressions are True or both are False, the result will be False. This operator is useful when you need to ensure that only one of the conditions is met, but not both.

Syntax:

Here is the syntax of the logical XOR operator in PowerShell:

<expression1> -xor <expression2>

Example:

Here is an example of how to use the logical XOR operator in PowerShell

$hasKey = $true
$hasPassword = $false

if ($hasKey -xor $hasPassword) {
    Write-Output "You can access the system."
} else {
    Write-Output "Access denied."
}

In this example, the script checks if the person has either a key or a password, but not both. If only one condition is True, the message “You can access the system.” is displayed.

Here is the output you can see the screenshot below, I executed the above PowerShell script using VS code.

PowerShell Logical Operators

Check out PowerShell Not Operator

PowerShell Logical Operators Examples

Now, let me show you a few examples of how to use the PowerShell logical operators.

Example-1: Combine Conditions

Logical operators can be used to combine multiple conditions in a single if statement in PowerShell. This allows you to create complex logical expressions to control the flow of your scripts.

Here is a complete example with the complete script.

$temperature = 30
$isSunny = $true
$isWeekend = $true

if ($temperature -gt 25 -and $isSunny -or $isWeekend) {
    Write-Output "It's a great day for outdoor activities."
} else {
    Write-Output "Maybe stay indoors today."
}

In this example, the script checks if the temperature is greater than 25 and it is sunny, or if it is a weekend. If any of these conditions are met, the message “It’s a great day for outdoor activities.” is displayed.

Example-2: Precedence of Operators

In PowerShell, the -and operator has higher precedence than the -or operator. This means that -and operations are evaluated before -or operations. You can use parentheses to define the order of evaluation explicitly.

Here is an example.

$condition1 = $true
$condition2 = $false
$condition3 = $true

if ($condition1 -or $condition2 -and $condition3) {
    Write-Output "Condition met."
} else {
    Write-Output "Condition not met."
}

In this example, the script evaluates the -and operation first ($condition2 -and $condition3), and then the -or operation. To change the order of evaluation, you can use parentheses:

if (($condition1 -or $condition2) -and $condition3) {
    Write-Output "Condition met."
} else {
    Write-Output "Condition not met."
}

Example-3: Conditional Execution

Logical operators are often used to control the execution of code blocks based on multiple conditions. This is particularly useful in if statements and loops.

Here is an example.

$userRole = "Admin"
$isAuthenticated = $true

if ($userRole -eq "Admin" -and $isAuthenticated) {
    Write-Output "Welcome, Admin!"
} else {
    Write-Output "Access denied."
}

In this example, the script checks if the user role is “Admin”

complete the rest of the article

and if the user is authenticated. If both conditions are met, the message “Welcome, Admin!” is displayed. Otherwise, “Access denied.” is shown.

Read -and Operator in PowerShell

Example-4: Filter Data

Logical operators can also be used to filter data in PowerShell. This is particularly useful when working with collections of objects, such as arrays or lists.

Here is a complete example.

$employees = @(
    @{ Name = "John"; Age = 30; Department = "HR" }
    @{ Name = "Jane"; Age = 25; Department = "IT" }
    @{ Name = "Doe"; Age = 40; Department = "Finance" }
    @{ Name = "Smith"; Age = 35; Department = "IT" }
)

$filteredEmployees = $employees | Where-Object { $_.Age -gt 30 -and $_.Department -eq "IT" }

$filteredEmployees | ForEach-Object {
    Write-Output "Name: $($_.Name), Age: $($_.Age), Department: $($_.Department)"
}

In this example, the script filters employees who are older than 30 and work in the IT department. The filtered employees are then displayed.

You can see the output in the screenshot below:

PowerShell Logical Operators Examples

Example-5: Loop Control

Logical operators can also be used to control loops, such as PowerShell while and for loops based on multiple conditions. Here is an example.

$count = 0
$maxCount = 10
$isRunning = $true

while ($count -lt $maxCount -and $isRunning) {
    Write-Output "Count: $count"
    $count++
    if ($count -eq 5) {
        $isRunning = $false
    }
}

In this example, the script runs a loop that increments a counter until the counter reaches the maximum count or the isRunning variable is set to False. The loop stops when either condition is met.

Conclusion

PowerShell logical operators allow you to create complex logical expressions, control the flow of your scripts, and make decisions based on multiple conditions.

Here, I have explained various logical operators in PowerShell with examples.

  1. AND Operator (-and): Ensures all conditions are met.
  2. OR Operator (-or): Ensures at least one condition is met.
  3. NOT Operator (-not): Reverses the logical state of an expression.
  4. XOR Operator (-xor): Ensures only one condition is met, but not both.

If you still have any questions, then submit a comment below.

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.