Combine Multiple Conditions in if Statement

Rohan Timalsina Apr 30, 2022
Combine Multiple Conditions in if Statement

If statement is one of the most useful commands in PowerShell. You can execute code blocks based on the evaluation of the statement.

Here is a simple example of an if statement.

if(2 -eq 2){
Write-Host "2 is equal to 2."

If the specified condition in parentheses () evaluates to $true, then it runs the command in the braces {}.


2 is equal to 2.

If the condition is $false, it will skip that code block. The above example uses one condition, but you can also evaluate multiple conditions in the if statement.

This tutorial will teach you to combine multiple conditions in PowerShell’s if statement.

Use Logical Operators to Combine Multiple Conditions in If Statement in PowerShell

The logical operators connect statements and expressions in PowerShell. You can use a single expression to test for multiple conditions by using them.

The supported logical operators in the PowerShell are -and, -or, -xor, -not, and !. For more information, see Logical Operators.

The following example uses the -and operator to connect two statements combined in the if statement. If the first condition 5 is less than 10 is true and the second condition 7 is greater than 5 is true, the Write-Host command will be executed.

if((5 -lt 10) -and (7 -gt 5 )){
Write-Host "The above conditions are true."


The above conditions are true.

Similarly, you can combine multiple conditions using logical operators in PowerShell’s if statement.

The following if statement executes the first command if the statement is true and the second command if the statement is false.

if(((10 -lt 20) -and (10 -eq 10)) -or ((15 -gt 5) -and (12 -lt 6))){
Write-Host "It is true."
Write-Host "It is false"


It is true.

You must put each set of conditions inside the parentheses (). Any logical operators can be used to connect statements according to your conditions.

We hope this tutorial helped you understand how to combine multiple conditions in the if statement in PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Statement