How to Use Multiple Conditions in PowerShell If Else Statement?

If you want to become an expert in PowerShell, then you should know how to use multiple conditions in the PowerShell if-else statement. I will show you how it works here with various examples.

As a PowerShell developer, you might be required to add multiple conditions in an if-else statement. So, let’s get started.

Before using multiple conditions in an if-else statement, here is what the basic if-else statement looks like:

if (<condition>) {
    # Code to execute if condition is true
} else {
    # Code to execute if condition is false
}

Here is an example:

$city = "New York"

if ($city -eq "New York") {
    Write-Output "The city is New York."
} else {
    Write-Output "The city is not New York."
}

In this example, the script checks if the $city variable is equal to “New York”. If it is, it outputs “The city is New York.” Otherwise, it outputs “The city is not New York.”

How to Use Multiple Conditions in PowerShell If Else Statement?

Now, let us see how to add multiple conditions in PowerShell if else statement. We can do this by using various logical operators, such as -and-or, and -not.

Logical AND (-and)

The -and operator in PowerShell allows you to combine two or more conditions, and the combined condition is true only if all individual conditions are true. Using the PowerShell —and operator, we can add multiple conditions in an if-else statement.

Syntax

if (<condition1> -and <condition2>) {
    # Code to execute if both conditions are true
}

Example

And here is an example, you can check out the script:

$state = "California"
$population = 39500000

if ($state -eq "California" -and $population -gt 30000000) {
    Write-Output "California has a population greater than 30 million."
} else {
    Write-Output "Either the state is not California or its population is not greater than 30 million."
}

In this example, the script checks if both conditions are true: the state is “California”, and its population is greater than 30 million. If both conditions are met, it outputs a message accordingly.

You can see the output in the screenshot below after I executed the above PowerShell using VS code.

Multiple Conditions in PowerShell If Else Statement

Read PowerShell If-Else String Comparison

Logical OR (-or)

Like the -and operator, you can also use the -or operator in PowerShell if else conditions.

The PowerShell -or operator allows you to combine two or more conditions, and the combined condition is true if at least one of the individual conditions is true.

Syntax

if (<condition1> -or <condition2>) {
    # Code to execute if at least one condition is true
}

Example

Here is an example.

$city = "Los Angeles"
$state = "California"

if ($city -eq "Los Angeles" -or $state -eq "California") {
    Write-Output "Either the city is Los Angeles or the state is California."
} else {
    Write-Output "Neither the city is Los Angeles nor the state is California."
}

In this example, the script checks if either the city is “Los Angeles” or the state is “California”. If at least one of these conditions is true, it outputs a message accordingly.

Look at the screenshot below I executed the above PowerShell script using VS code.

How to Use Multiple Conditions in PowerShell If Else Statement

Read PowerShell Do While Loop

Logical NOT (-not)

The -not operator negates a condition, making it true if the original condition is false and vice versa. This is another way to add multiple conditions to an if-else condition in PowerShell.

Syntax

if (-not <condition>) {
    # Code to execute if the condition is false
}

Example

Here is an example of how to use the -not operator in PowerShell if-else statements.

$state = "Texas"

if (-not ($state -eq "California")) {
    Write-Output "The state is not California."
} else {
    Write-Output "The state is California."
}

In this example, the script checks if the state is not “California”. If the condition is true (i.e., the state is not “California”), it outputs a message accordingly.

powershell if else statement multiple conditions

Read PowerShell Functions: Return Values and Multiple Values

Combine Multiple Conditions in an if-else Statement

Now, let me show you how to combine multiple conditions in an if-else statement in PowerShell.

You can combine multiple conditions using a mix of -and-or, and -not operators to create complex logical expressions in PowerShell.

Example: Combine AND and OR

Here is an example of how to combine AND and OR conditions in a PowerShell if-else statement.

$city = "San Francisco"
$state = "California"
$population = 883305

if (($city -eq "San Francisco" -and $state -eq "California") -or $population -gt 1000000) {
    Write-Output "Either the city is San Francisco and the state is California, or the population is greater than 1 million."
} else {
    Write-Output "Neither the city is San Francisco and the state is California, nor is the population greater than 1 million."
}

In this example, the script checks if either the city is “San Francisco” and the state is “California”, or the population is greater than 1 million. If either of these combined conditions is true, it outputs a message accordingly.

Example: Use Multiple AND Conditions

Here is another example where you can use multiple AND conditions in a PowerShell if else statement.

$city = "Chicago"
$state = "Illinois"
$population = 2716000

if ($city -eq "Chicago" -and $state -eq "Illinois" -and $population -gt 2000000) {
    Write-Output "The city is Chicago, the state is Illinois, and the population is greater than 2 million."
} else {
    Write-Output "One or more conditions are not met."
}

In this example, the script checks if all three conditions are true: the city is “Chicago”, the state is “Illinois”, and the population is greater than 2 million. If all conditions are met, it outputs a message accordingly.

Read PowerShell If Else Statement to Check if a Number is Between Two Values

PowerShell if two conditions are true

Let’s move on to examples where we need to check if two conditions are true in a PowerShell if else statement.

Here is an example where we check two true conditions in PowerShell if else statement.

$state = "California"
$city = "Los Angeles"

if ($state -eq "California" -and $city -eq "Los Angeles") {
    Write-Output "The city is Los Angeles in California."
}

In this example, the script checks if $state is “California” and $city is “Los Angeles”. If both conditions are true, it outputs “The city is Los Angeles in California.”

Here is another example.

$age = 25
$isCitizen = $true

if ($age -ge 18 -and $isCitizen -eq $true) {
    Write-Output "The person is eligible to vote."
} else {
    Write-Output "The person is not eligible to vote."
}

In this example, the script checks if $age is greater than or equal to 18 and $isCitizen is true. If both conditions are true, it outputs “The person is eligible to vote.” Otherwise, it outputs “The person is not eligible to vote.”

Read How to Use Exclamation Mark in PowerShell If Statements?

PowerShell If-Else multiple conditions examples

Here are a few examples of how to use multiple conditions in the PowerShell if-else statement.

Example-1: Multiple Conditions in Nested If-Else Statement

Here is an example of checking multiple conditions in a nested if-else PowerShell statement:

$state = "Texas"
$city = "Houston"
$population = 2300000

if ($state -eq "Texas") {
    if ($city -eq "Houston" -and $population -gt 2000000) {
        Write-Output "Houston is a large city in Texas."
    }
}

In this example, the script first checks if $state is “Texas”. If this condition is true, it then checks if $city is “Houston” and $population is greater than 2,000,000. If both nested conditions are true, it outputs “Houston is a large city in Texas.”

Let’s explore some practical scenarios where checking two conditions is useful.

Example-2: Multiple Condition to Check User Details in an if-else statement

Here is an example where you need to authenticate a user based on their username and password in PowerShell:

$username = "john_doe"
$password = "securePassword123"

if ($username -eq "john_doe" -and $password -eq "securePassword123") {
    Write-Output "User authenticated successfully."
} else {
    Write-Output "Authentication failed."
}

In this example, the script checks if both the username and password match the expected values. If both conditions are true, it outputs “User authenticated successfully.” Otherwise, it outputs “Authentication failed.”

Example-3: Multiple Conditions to Check If a File Exists and Is Not Empty

Here is another example, where we can use multiple conditions in an if-else statement to check if a file exists and is not empty in PowerShell.

$filePath = "C:\Bijay\report.txt"

if (Test-Path $filePath -and (Get-Item $filePath).Length -gt 0) {
    Write-Output "The file exists and is not empty."
} else {
    Write-Output "The file does not exist or is empty."
}

In this example, the script uses Test-Path to check if the file exists and (Get-Item $filePath).Length to check if the file is not empty. If both conditions are true, it outputs “The file exists and is not empty.” Otherwise, it outputs “The file does not exist or is empty.”

Conclusion

If you want to write complex PowerShell scripts, then you should know how to use multiple conditions in an if-else in PowerShell.

In this tutorial, I have explained how to use logical operators like -and-or, and -not, and by combining conditions to add multiple conditions in the PowerShell if else statement.

I hope the multiple conditions in PowerShell if-else statement will help you to learn this.

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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