PowerShell: Loop Do While and Do Until [Quick Guide]

5:00 pm November 28, 20169889

I. introduction

this tutorial PowerShell is intended to you explain the operation of Do loops… Until and Do… While in PowerShell , because they have an important role to play in your scripts.

With a syntax very close or identical, these two loops have a different logic. For our great pleasure, because it offers more possibilities for script 🙂

the goal is to explain simply and synthetic way the functioning of these two loops PowerShell.

“” II. Do Until with PowerShell

the Do-Until in PowerShell loop works according to the logic next:

Do{code} Until(la condition soit vraie) 

in other words:

 play {code} until (the condition to be true) 

which may be interesting especially for a check in the “Do” and as soon as the State expected happens and go out of the loop. There are many cases of the use of this type of loop, it may use time delay in a script to wait that toggle a State on the State expected before continuing.

Let’s take a simple example, with a variable $i will increment by 1 whenever we replay a loop tour. You want to play the code until $i is equal to 10. Here’s the code:

 $i = 0 Do {$i ++ Write-Host "i = $i"} Until($i-eq 10) Write-Host "Loop complete!" 

You will see that it works perfectly, as soon as $i is equal to 10, we’re out of the loop.

powershell-do-until-exemple

You can also add several conditions, recognize that we also have a $j variable that increments by 1 at each turn of the loop… We’ll initialize this variable to 5 while $i is initialized to 0. We wish to break out of the loop as soon as $i or $j arrives at 10:

 $i = 0 $j = 5 Do {$i ++ $j ++ Write-Host "i = $i and d = $j"} Until (($i-eq 10) - Gold ($j - eq 10)) Write-Host "one of the two variables arrived at 10 so we're out of the loop!" 

You will notice one frame each condition by parentheses in the same way that we can do in a if , for example. In addition, then used the operator – gold but we could also use another operator such that – and, according to what you want to do.

powershell-do-until-multiple-conditions

III. Do While with PowerShell

the Do-while loop in PowerShell works the opposite of the Do-Until loop, since it follows the logic next:

Do{code} While(la condition est vraie) 

in other words:

 play {code} while (condition is true) 

this loop is similar to a conventional While loop, except for the syntax that will be different. In the case of Do While you say “I play the code as long as the condition is true” as in the loop While you say “as long as the condition is true, I play. ‘ Everything is a question of order in the syntax.

If one would like to take as an example, a variable $i that is incremented as we did in the example of the Do-Until we could but it should adapt the condition. Because if we take as a condition “$i = 10” This means that if we set $i to 0, it will not equal to 10 then go leave the loop. Have an equivalent to the condition “$i less than or equal to 10.

In french this gives:

  • Do-Until: increments $i 1 until $i is equal to 10
  • Do-While: increments $i 1 as $i is less than or equal to 10

I hope that you understand the difference.

Let’s take an example with the Do-While, where you are going to increment 1 $i as long as there is a ‘notepad’ (Notepad) running on the machine process. First, the condition will be:

 Get-Process - Name notepad - ErrorAction SilentlyContinue 

specified the “ErrorAction” in “SilentlyContinue” for not an error in the console when the process will not be found that is to say when we will have closed the application. For the example code, it’s simple:

 $i = 0 Do {$i ++ Write-Host $i} While(Get-Process-Name notepad-ErrorAction SilentlyContinue) Write-Host "Notepad is closed, we can continue." 

You can see in the console that is out of the loop as soon as I close the notebook open on my machine:

powershell-do-while-exemple

it is worth noting that to add several conditions in the Do-While, it’s the same syntax as for a if or a Do-Until .

In summary…

With Do-Until we play the code until the condition is true, that is to say that a variable reaches a specific value, for example, while with Do-While we play the code as long as the condition is true.