Getty Images

Do-While vs. Do-Until in PowerShell: What's the difference?

PowerShell scripts have several loop options to make automation easy. Learn the difference between Do-While and Do-Until loops and choose which one works best for you.

As a scripting language, PowerShell provides various loops to help with automation tasks. Understanding Do-Until loops vs. Do-While loops is essential to utilize them effectively.

The Do-Until and Do-While loops only differ syntactically in the keywords they use. Choosing between the two comes down to how you want the loop to evaluate your condition. The Do-Until loop will continue to loop while the condition is false. Do-While will continue to loop while the condition is true. Knowing this will help you write more efficient and straightforward scripts.

Do-Until

The Do-Until loop in PowerShell is designed to repeatedly execute a block of code until a specific condition is met. Like Do-While, the code will be executed once before the condition is evaluated. Unlike Do-While, the condition needs to resolve to false for the code to continue executing.

In Figure 1, a service is started and then the loop waits for one second before it checks to see if the service's status is equal to "Running." If the service is not running, then the loop will run again until the service is running.

Do-Until loop code syntax.
Figure 1. This Do-Until loop repeats until the conditions are true.

Do-While

The Do-While loop in PowerShell is also designed to execute a block of code repeatedly until a specific condition is met. However, unlike the Do-Until loop, Do-While will continue to loop while the condition returns true and will only exit the loop when the condition returns false.

An example of the Do-While code syntax.
Figure 2. The syntax for Do-While is similar to that of Do-Until.

Figure 2 is similar to the Do-Until example, but it operates differently. In this case, the script will enter the loop, sleep for one second and then check to see if the specified service is running. If it is, it will continue to sleep and only exit the loop when the service stops. This approach is useful if you need to monitor a service.

Comparing to the While loop

Like Do-Until and Do-While, the While loop is designed to continually execute a block of code until the specified condition is met. With the While loop, the condition is evaluated before any code is executed. Therefore, it is possible that the code inside of a While loop might never execute. If you need the code to execute at least once, start with either a Do-Until or a Do-While loop.

Bringing them all together

To illustrate the differences between these three loops, we can combine them all into a simple service monitor script.

An example of all three commands being used in a single script.
All three loops can form a service monitor script.

In the example in Figure 3, the While loop will execute until the script is halted since $true always evaluates to true. The Do-Until loop will wait until the service starts, and the Do-While will wait until it stops. Once the service has stopped, the process will start over again. This is a simple way to run a script that will continually start a service that is stopping.

Anthony Howell is an IT expert who is well-versed in multiple infrastructure and automation technologies, including PowerShell, DevOps, cloud computing, and the Windows and Linux operating systems.

Dig Deeper on IT systems management and monitoring