Table of Contents
We use an
if
statement with logical operators and comparison operators to combine multiple conditions in PowerShell. We also use parentheses represented by (()
) to group conditions and control the evaluation order. So, it is essential to know what logical, arithmetic and comparison operators we have, what they mean and what they can do. See the following tables for quick understanding.
All the comparison, logical, and arithmetic operators and their precedence are explained in a tabular form after the code examples.
Using if
with Logical & Comparison Operators
Use the if
statement with one logical operator (-and
) and two comparison operators (-gt
and -lt
) to combine multiple conditions in PowerShell.
1 2 3 4 5 6 7 |
$number1 = 3 $number2 = 11 if ($number1 -lt 4 -and $number2 -gt 10){ echo "'$number1' is less than '4' while '$number2' is greater than '10'" } |
1 2 3 |
'3' is less than '4' while '11' is greater than '10' |
The above if
statement will only be executed if both conditions are True
; let’s take another example where both conditions are unsatisfied.
1 2 3 4 5 6 7 |
$number1 = 4 $number2 = 11 if ($number1 -lt 4 -and $number2 -gt 10){ echo "'$number1' is less than '4' while '$number2' is greater than '10'" } |
We will not get any output this time because both conditions still need to be fulfilled.
Use the if
statement with one logical operator (-or
) and one comparison operators (-eq
) to combine multiple conditions in PowerShell.
1 2 3 4 5 6 7 8 9 10 |
$firstname = "John" $lastname = "Williamson" if ($firstname -eq "John" -or $lastname -eq "Williamson") { echo "Condition satisfied." } else{ echo "Condition not satisfied." } |
1 2 3 |
Condition satisfied. |
Here, the if
block will only be executed if at least one or all of the specified conditions will be True
; otherwise, the else
block will be executed. Let’s modify the example where no condition is True
.
1 2 3 4 5 6 7 8 9 10 |
$firstname = "Martin" $lastname = "Guptill" if ($firstname -eq "John" -or $lastname -eq "Williamson") { echo "At least one or all conditions are satisfied." } else{ echo "No condition is satisfied." } |
1 2 3 |
No condition is satisfied. |
Use the if
statement with two logical operators (-not
and -or
) and one comparison operator (-lt
) to combine multiple conditions in PowerShell.
1 2 3 4 5 |
if (-not 4 -le 2 -or 4 -lt 2) { echo "Do something." } |
1 2 3 |
Do something. |
Focus! this example needs your attention to understand. First, it’s essential to know the precedence of each operator used in the above code snippet. So, -not
has the highest precedence and will be evaluated first, while -or
has the lowest precedence and will be considered last. Now, we have two -lt
operators having the same precedence, but the -lt
operator, immediately after the -not
operator, will be evaluated first.
Here is the execution flow:
4 -lt 2
=False
-not 4 -lt 2
=True
- Another
4 -lt 2
=False
- Result of step-2
-or
step 3 =True -or False
=True
It is the best time to introduce the parentheses, which control the evaluation order and increase the code readability. The expressions within the ()
will be evaluated first. If we have nested parentheses, the inner ones will be considered first. See the following example.
1 2 3 4 5 |
if ((-not (4 -le 2)) -or (4 -lt 2)) { echo "Do something." } |
1 2 3 |
Do something. |
Similarly, we can use other operators given in the following tables.
Comparison Operators and their Precedence
All comparison operators return a Boolean value (True
/False
), True
if the condition is fulfilled; otherwise, False
. The point is which operator will be evaluated first if we have multiple at a time. In that case, they will be assessed based on the precedence given below from highest to lowest.
-eq -ne -gt -ge -lt -le
-like -notlike
-match -notmatch
-in -notIn
-contains -notcontains
The above group of comparison operators have the same precedence. Note that their case-sensitive (prefixed with c
, e.g. -cmatch
) and explicitly case-insensitive (prefixed with i
, e.g. -imatch
) variants also have equal (identical) precedence. The operator having the highest precedence (priority) will be evaluated first, while operators having the same priority will be assessed from left to right as they appear in the given expression.
In the examples presented in the above table, the array operatory represented by
@()
was used to create arrays.
Further reading:
Logical and Arithmetic Operators and their Precedence
All logical operators return a Boolean value, True
if the specified condition is fulfilled; otherwise, False
. On the other hand, arithmetic operators (bitwise operators) return a numeric value based on the operator. These operators also have precedence, given below from highest to lowest priority.
-band -bnot -bor -bxor
-and -or -xor
The above group of operators have equal precedence. Therefore, the operators with the highest priority will be evaluated first. In contrast, operators with the same precedence will be assessed from left to right as they occur in the given expression.
-not
has the highest precedence in all comparison, arithmetic, and logical operators. It is also important to note that comparison operators will be evaluated earlier than the arithmetic and logical operators because they have the highest precedence. You can also control the evaluation order using parentheses.
You can check the operator’s priority here.
That’s all about how to combine multiple conditions in if statement in PowerShell.