PowerShell: How to Use If Statement with Multiple Conditions


You can use the following methods in PowerShell to create an if statement that checks if multiple conditions are met:

Method 1: If Statement with -and operator

if ($first -gt 5 -and $second -gt 8) {
    echo "Both conditions are met"} else {
    echo "Both conditions are not met"
}

This particular function checks if the $first variable has a value greater than 5 and if the $second variable has a value greater than 8.

Method 2: If Statement with -or operator

if ($first -gt 5 -or $second -gt 20) {
    echo "At least one condition is met"} else {
    echo "Neither condition is met"
}

This particular function checks if either the $first variable has a value greater than 5 or the $second variable has a value greater than 20.

Method 3: If statement with -xor operator

if ($first -gt 5 -xor $second -gt 8) {
    echo "Exactly one condition is met"} else {
    echo "Not exactly one condition is met"
}

This particular function checks if either the $first variable has a value greater than 5 or the $second variable has a value greater than 8, but not both.

The following examples show how to use each method in practice.

Example 1: If Statement with -and Operator

We can use the -and operator in PowerShell within an if statement to check if two conditions are met.

For example, we can use the following syntax to check if two variables have values greater than specific numbers:

$first = 10
$second = 15

if ($first -gt 5 -and $second -gt 8) {
    echo "Both conditions are met"} else {
    echo "Both conditions are not met"
}

The following screenshot shows how to use this syntax in practice:

PowerShell if and statement

This if statement returns “Both conditions are met” because:

  • The $first variable does have a value greater than 5
  • The $second variable does have a value greater than 8

Example 2: If Statement with -or Operator

We can use the -or operator in PowerShell within an if statement to check if at least one condition is met.

For example, we can use the following syntax to check if at least one variable has a value greater than a specific number:

$first = 10
$second = 15

if ($first -gt 5 -or $second -gt 20) {
    echo "At least one condition is met"} else {
    echo "Neither condition is met"
}

The following screenshot shows how to use this syntax in practice:

PowerShell if or statement

This if statement returns “At least one condition is met” because:

  • The $first variable does have a value greater than 5
  • The $second variable does not have a value greater than 20

Only one of the conditions was met, so the if statement correctly tells us that at least one of the conditions is met.

Example 3: If Statement with -xor Operator

We can use the -xor operator in PowerShell within an if statement to check if exactly one condition is met.

For example, we can use the following syntax to check if exactly one variable has a value greater than a specific number:

$first = 10
$second = 15

if ($first -gt 5 -xor $second -gt 8) {
    echo "Exactly one condition is met"} else {
    echo "Not exactly one condition is met"
}

The following screenshot shows how to use this syntax in practice:

PowerShell if xor statement

This if statement returns “Not exactly one condition is met” because:

  • The $first variable does have a value greater than 5
  • The $second variable does have a value greater than 8

Since both conditions are met the if statement correctly tells us that not exactly one condition is met.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Return Multiple Values from Function
PowerShell: How to Replace Every Occurrence of String in File
PowerShell: How to Replace Text with Wildcard

Leave a Reply