Do While, While and Do Until in PowerShell

I used this again in a few scripts while creating packages in Intune for a customer, where I had to wait for specific tasks to finish. In this brief blog post, I will outline how it works and discuss its use cases.

When should I use it?

While or Do While is useful when you want to continue if the condition is True or met, or when you want to run a particular action only when the condition is met. Depends on the use case, but both allow you to use them as a trigger.

Do Until is almost the same, but it is Do While inverted. It continues running until the condition is True, as opposed to Do While, which continues running until the condition is False.

Examples

As always, reading something doesn’t immediately give you an idea of how that works in real life. I will show you a few examples below 🙂

Do While

This will run a specific ScriptBlock continuously until a condition is met. In the example below, I wait for a particular word (Done) in a log file before continuing.

You can see that I use Get-Content to read the contents of c:\temp\psif.txt and filter for the word ‘Done‘. Because of the while loop, which contains ‘-not‘, it will wait until the word ‘Done’ is added to the logfile being read. (I added the “Start-Sleep -Seconds 5” to give it some time and not to stress the system too much ;-). The while loop will switch from True (since it’s not being found) to False (since it has been found), leave the loop, and then continue with the rest of the script.

When I added the word ‘Done’ to the psif.txt, the loop stopped:

Note: This Do While loop will always run at least once to determine if the Scriptblock matches the while statement and to decide whether it should continue or stop.

While

Using While will only run the Scriptblock when a condition is met. In the example below, I use it to wait until a specific host responds to a ping.

You can see that it attempted to ping 192.168.168.134 three times without success. Then, the VM was up and running, and when the “While” condition was no longer met, it left the Scriptblock to continue the script.

Do until

This will continue running as long as the condition is False; when it becomes True, it stops executing the ScriptBlock. Using the same example as above, checking if a VM is online, it looks like this:

Extra: Using While until you Break/Ctrl-C

I also wrote about this in a previous blog post. I use While ($true) {} to keep running something until I press Ctrl-C. Additionally, you can use Break in the script to stop execution (because $true is no longer valid). For example, this will get my last blog posts and refresh them every 10 seconds until it can’t fetch them, and Break will quit the script.

I changed the URL to powershellisfunny.com to force an error and let the script Break because of that in this example below:

Wrapping up

And that’s how you can use Do While, While, and Do Until to your benefit 😉 Have a lovely weekend!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.