Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about understanding and using the Do…Until loop.
Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are busy getting ready for TechEd 2014 North America, which will be in Houston, Texas this year. It kicks off this Sunday with registration and preconference sessions. This year, TechEd sold out several weeks prior to the event, so there is lots of excitement built up around this event. The Scripting Wife posted her Ideal TechEd Schedule, the Meet and Greet Schedule for the Scripting Guys booth, and a map so you can Find the Scripting Guys Booth at TechEd 2014.
A major point of confusion amongst beginning scripters, regardless of the language, is the difference between a Do…Until loop and a Do…While loop.
Note Also see yesterday’s post, PowerShell Looping: Understanding and Using Do...While.
In one respect, Do … Until and Do … While are opposites. This is due to the position of the condition to be evaluated. Whereas, a Do…While loop continues processing WHILE a condition evaluates as True, a Do…Until loop processes UNTIL a condition becomes True. This is a subtle difference that can lead to seriously difficult errors to detect in a complicated Windows PowerShell script.
In yesterday’s post, I talked about using the Do...While loop. In one of the scripts from that post, I evaluate the value of a variable. If the value of $a is less than or equal to the number 5, the script block continues to process. But if the initial value of $a is greater than the 5, the script block still runs one time. This is because it is bottom evaluated. Here is a screenshot of that process:
If I change this from a Do…While to a Do…Until loop, the script looks like this:
$a = 0 #if greater than 5 runs forever
#if less than 5 runs only once
"Starting Loop $a"
"Now `$a is $a"
} Until ($a -le 5)
The script block runs one time because it runs until the value of $a is less than or equal to 5. The output is shown here:
If I increase the value of $a to 9, notice that the script goes into an infinite loop. This is because the script runs the script block, and then it evaluates the condition. Because the condition will never become True, the script block continues to run, and run, and run. This is shown here:
In summary, both Do…While and Do…Until are bottom evaluated. Therefore, the script block runs at least one time. Do…While runs as long as a condition is True. When that condition becomes False, it ceases to run. However, if the condition is always False, it will still run one loop.
Do…Until runs until a condition becomes True. That is, it runs as long as a condition is False. It also evaluates at the bottom of the loop; and therefore, it will also run at least one time. If a condition never becomes True, the script goes into an endless loop.
In my example, 9 is never less than or equal to 5; and therefore, the condition will never become True. The script block went into an endless loop, and I had to stop the script by pressing the red square in the ISE. In the Windows PowerShell console, I would press Ctrl+C.
There are three similar tools: While, Do…While, and Do…Until. Each has its own usage and purpose in a script.
That is all there is to using Do…Until. Join me tomorrow when I will talk about Windows PowerShell control statements in looping.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Is there a difference between these two?Do ... While ($condition)Do ... While (! $condition)
Thanks for this. I have never actually used Do…Until. Very helpful tips.