How to Use Multiple Conditions in Do-While Loop in PowerShell?

Recently, while working on a do-while loop in PowerShell, I got a requirement to use multiple conditions. I tried a few examples. In this tutorial, I will show you how to use multiple conditions in a do-while loop in PowerShell.

Do-While Loop in PowerShell

The Do-While loop in PowerShell executes a block of code at least once before evaluating the condition. The syntax is like below:

Do {
    # Code to execute
} While (condition)

The loop continues as long as the condition is true. If the condition is false, the loop terminates.

I have already written a complete tutorial on PowerShell Do While Loop. Here, we will focus on working with multiple conditions.

Multiple Conditions in Do-While Loop in PowerShell

You may need to evaluate multiple conditions in many scenarios to determine whether the loop should continue. In a PowerShell do-while loop, we can use multiple conditions using logical operators such as -and (logical AND) and -or (logical OR).

Example 1: Using Logical AND

The first example I will show you is how to use logical AND in a do-while loop in PowerShell.

Here, we want the loop to continue only if two conditions are true. Suppose we want to keep prompting the user for input until they enter a number greater than 10 and less than 20.

Do {
    $input = Read-Host "Enter a number between 10 and 20"
} While ($input -le 10 -or $input -ge 20)
Write-Host "You entered a valid number: $input"

In this example, the loop will continue until the user enters a number within the specified range. The condition $input -le 10 -or $input -ge 20 ensures that the loop continues if the input is not within the desired range.

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

Multiple Conditions in Do-While Loop in PowerShell

Read Concatenate Strings Inside Loops in PowerShell

Example 2: Using Logical OR

Let’s consider another example where we want the loop to continue if at least one of two conditions is true. For example, we want to keep asking the user for their age until they enter a number greater than 0 or less than 120.

Here is the PowerShell script with the do-while loop with a logical OR.

Do {
    $age = Read-Host "Enter your age"
} While ($age -le 0 -or $age -ge 120)
Write-Host "You entered a valid age: $age"

In this example, the loop continues as long as the age is not within the realistic range of 1 to 119. The condition $age -le 0 -or $age -ge 120 ensures that unrealistic ages keep the loop running.

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

PowerShell Do-While Loop Multiple Conditions

Example 3: Combine Multiple Conditions

You can also combine multiple conditions using both logical AND and OR operators. Suppose we want to prompt the user for a password until it meets the following criteria:

  1. The password length must be at least 8 characters.
  2. The password must contain at least one digit.

Here is the do-while loop in PowerShell with multiple conditions like AND and OR.

Do {
    $password = Read-Host "Enter a password"
    $hasDigit = $password -match '\d'
    $isLongEnough = $password.Length -ge 8
} While (-not ($hasDigit -and $isLongEnough))
Write-Host "You entered a valid password."

In this example, the loop continues until the password meets both conditions. The condition -not ($hasDigit -and $isLongEnough) ensures that the loop only exits when both conditions are true.

In the screenshot below, you can see that I executed the above PowerShell script, which gave the output after I entered a password.

How to Use Multiple Conditions in Do-While Loop in PowerShell

Conclusion

I hope you learn how to use multiple conditions in a Do-While loop in PowerShell. I have explained how to use multiple conditions in a do-while loop in PowerShell using logical operators, such as -and and -or, etc.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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