Combine Multiple Conditions in if Statement in PowerShell

Combine multiple conditions in if statement in PowerShell

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.

The above if statement will only be executed if both conditions are True; let’s take another example where both conditions are unsatisfied.

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.

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.

Use the if statement with two logical operators (-not and -or) and one comparison operator (-lt) to combine multiple conditions in PowerShell.

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:

  1. 4 -lt 2 = False
  2. -not 4 -lt 2 = True
  3. Another 4 -lt 2 = False
  4. 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.

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.

  1. -eq -ne -gt -ge -lt -le
  2. -like -notlike
  3. -match -notmatch
  4. -in -notIn
  5. -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.

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.

  1. -band -bnot -bor -bxor
  2. -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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *