Updated March 6, 2023
Introduction to PowerShell do while
The do-While loop in PowerShell is one of the iterative loops (for, foreach, while, do-while) that runs the content inside the block multiple times based on the condition provided, runs until the condition is true, and terminates when the condition becomes false, moreover it ensures that the loop will execute at least once and this loop is opposite of the Do-Until loop which runs until the condition is True and stops when the condition becomes false. Most of the times conditions are checked with the conditional operators.
Syntax
Do-While loop syntax
do {
Statement 1
Statement 2
............
............
Statement N
} while (condition)
Do-Until loop syntax
do {
Statement 1
Statement 2
............
............
Statement N
} until (condition)
Flowchart
Flow chart for the Do-While loop.
<image>
How do-while loop works in PowerShell?
Do-While loop ensures that the loop will be executed at least once by entering the loop the first time and then later it checks the condition and enters into the loop if the condition is True or vice versa. For example,
do {
Write-Output "`nThis loop is Guaranteed to execute the first time"
} while ($false)
Output:
If you see the above output, the loop is executed even the condition is false because it is by its design that it enters into the loop first, and then for the next run it checks the While condition until it becomes false.
If we use $true in the condition, the loop will become infinite and it will never terminate because the condition will never become false.
On the contrary, the opposite loop do-until executes until the condition becomes true. For example,
do {
Write-Output "`nUntil is Guaranteed to execute first time"
} until ($true)
Output:
If we have used $false condition here, the loop will never be exited because the condition inside the while will never become false.
Difference between While, Do-While loop and Do-Until loop.
While Loop:
$a = 100
While ($a -gt 110) {
$a++
Write-Output "This won't be printed"
}
In the above example of while loop, the output won’t be printed
Do-While Loop:
$a = 100
do {
$a++
Write-Host "`nThis line will be printed once only" -BackgroundColor DarkGreen
} while ($a -gt 110)
Output:
In the above example, the output will be printed once only because the condition becomes false.
Do-Until Loop:
$a = 100
do {
$a++
Write-Host "`nThis line will be printed 10 times" -BackgroundColor DarkGreen
} until ($a -gt 110)
Output:
Examples
Here are the following examples mention below
Example #1 – Simple Do-While loop.
$i = 0
do {
$i
$i++
} while ($i -lt 5)
Output:
In the above example, the $i variable is initialized with 0 and inside the do-while loop the value increments by one at every iteration, and the loop terminates when the $i value reaches 5.
Example #2 – Do-While loop with multiple conditions.
$i = 0
$j = 0
do {
Write-Output "i = $i, j= $j"
$i++
$j = $j + 2
} While(($i -lt 5) -and ($j -lt 15))
Output:
In the above example, if any of the conditions becomes false, the loop terminates.
See the another example with multiple conditions.
$i = 0
$j = 0
do {
Write-Output "i = $i, j= $j"
$i++
$j = $j + 2
} While(($i -lt 5) -or ($j -lt 15))
Output:
In the above example, if both of the conditions become false then the loop is terminated and for any single true condition, it enters into the loop.
Example #3 – Do-While loop with an array.
We can iterate through each array items with Do-While loop. In the below example,
$animals = @("Cow","Dog","Cat","Elephant")
$i = 0
do{
$animals[$i]
$i++
}while($i -lt $animals.Length)
Output:
In this example, each item under the Animals array is printed because the $i value starts from 0 and continues until the length of the Animal array.
Example #4 – Do-While loop with Break
We can terminate the Do-While loop using the Break statement. In this example, we need if the value of the array meets the specific value then the loop should be terminated. An example is shown below.
$i = 0
do {
$i
$i = $i + 2
if($i -eq 10){ break }
} while ($i -lt 20)
Output:
When the $i value becomes 10, the do-while loop is terminated automatically.
Example #5 – Do-While loop with Continue
To skip the particular iteration, the Continue keyword is used. We can use the Continue keyword with the Do-While loop as shown below.
$i = 0
do{
if($i -eq 10) {
$i = $i + 2
Continue
}
$i
$i = $i + 2
}while($i -lt 20)
Output:
In the above example, when the $i value becomes 10, next all the commands will be skipped during that iteration so here the output 10 isn’t displayed.
Example #6 – Do-While loop with User Input prompt.
Sometimes we need the input values from the User and we need to continue until the user enters the correct input. You can use the Do-While loop for that. For example,
$choice = ""
do{
Write-Output "*********************************"
Write-Output "`tPress 'R' to Restart VM"
Write-Output "`tPress 'S' to Shutdown VM"
Write-Output "*********************************"
$choice = Read-Host 'Enter Choice '
switch($choice){
'r' { "Reboot selected"}
's' {"Shutdown selected"}
}
}while(($choice -ne 'r') -and ($choice -ne 's'))
The above script will prompt for the user input and continues until the $choice value doesn’t become ‘R’ or ‘S’.
Output:
Conclusion
Like any other loop function (For, Foreach, While), the Do-While loop is also very helpful in PowerShell or various scripting languages. You can use this loop for various programs where you need the loop needs at least one-time execution and later checks the condition. For example, you can use the Test-WSMan command after server reboot inside this loop to check if the WINRM connectivity is established or not.
Recommended Articles
This is a guide to PowerShell do while. Here we discuss How do-while loop works in PowerShell along with the examples and outputs. You may also have a look at the following articles to learn more –