PowerShell Loops Featuring Do.. While… Until
PowerShell has a variety of looping structures to iterate instructions, this page focuses on the Do…While conditional construction.
One of the key jobs in scripting is looping, let us see what each of the keywords ‘Do’, ‘While’ and ‘Until’ have to offer when tackling a repetitive job.
PowerShell Do…While Loops
Example: Waiting for User Input to Guess a Word
These two examples prompt the user to guess a word. ‘Do’ initiates ‘Read-Host’. ‘If and Else’ deal with the logic of a correct, or incorrect guess; the ‘Do’ section loops until the user guesses correctly, or gives-in and types ‘n’.
Do Until
The logic of this loop is to execute the Do {block} until (the condition is true). As usual in PowerShell, pay close attention to the style of brackets, the curly brackets or parentheses are guiding you to write the correct code.
Clear-Host
$strPassword ="123"
$strQuit = "Not yet"
Do {
$Guess = Read-Host "`n Guess the Password"
if($Guess -eq $StrPassword)
{" Correct guess"; $strQuit ="n"}
else{
$strQuit = Read-Host " Wrong `n Do you want another guess? (Y/N)"
}
} # End of 'Do'
Until ($strQuit -eq "N")
"`n Ready to do more stuff..."
Trap: Don’t try: Until ($strQuit = “N”). What you need here is -eq, this is PowerShell’s way of comparing two values.
Do While
The logic of ‘Do … While’ is the opposite of the Do Until. {Execute the block statement} while the (condition is true)
Clear-Host
$strPassword ="house"
$strQuit = "Guess again"
Do {
$Guess = Read-Host "`n Guess the Password"
if($Guess -eq $StrPassword)
{" Correct guess"; $strQuit ="n"}
else{
$strQuit = Read-Host " Wrong `n Do you want another guess? (Y/N)"
}
} # End of 'Do'
While ($strQuit -ne "N")
"`n Ready to do more stuff..."
Note 1: There are only two significant difference between these two examples:
Until ($strQuit -eq “N”)
While ($strQuit -ne “N”)
PowerShell’s ‘While’ on Its Own – No Explicit ‘Do’
In some ways this method employs the more traditional approach with the (condition) followed by the {Block command}. Clearly, PowerShell’s While loop is simpler and more basic than the ‘Do … While’ construction above.
Clear-Host
$strPassword ="house"
$strQuit = "Guess again"
While ($strQuit -ne "N") {
$Guess = Read-Host "`n Guess the Password"
If($Guess -eq $StrPassword)
{" Correct guess"; $strQuit ="n"}
else{
$strQuit = Read-Host " Wrong `n Do you want another guess? (Y/N)"
}
} # End of block statement
"`n Ready to do more stuff..."
Note 2: In this example the ‘While’ clause is at the beginning instead of the end.
Guy Recommends: Network Performance Monitor (FREE TRIAL)
SolarWinds Network Performance Monitor (NPM) will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM on a 30-day free trial.
PowerShell’s Simple ‘For’ Loop
PowerShell provides four or five different looping constructions, here is a simple method using the keyword ‘For’. As usual there is a (condition) and {Block Statement}.
The speciality of this loop is the <init> and <repeat> sections.
Here is the syntax:
For (<init>; <condition>; <repeat>) {<command_block>}
Example: Times Table for 125
For ($i = 125; $i -le 1000; $i+=125) { $i }
For (<init> ; <condition>; <repeat>) { 125 * 1,2,3,…8}
One side-effect of the For loop is that it always returns the <init> before it tests for the condition. The <repeat> modifies the $i variable, which is then tested inside the <condition> of the next cycle.
Researching PowerShell’s Loops
Clear-Host
Get-Help about_For
# Also
# Get-Help about_While
See also the Foreach-Object Cmdlet
Summary of PowerShell Do.. While… Until
PowerShell supplies at least four types of loops to cater for a variety of script logic. Do keep an eye on the brackets, for instance the conditional elements, (parenthesis brackets) for the condition and {braces} for the command block.
If you like this page then please share it with your friends
See more Windows PowerShell flow control examples
• PowerShell Switch Statement • PowerShell Real-life Techniques • Free Permissions Analyzer
• Differences between For, ForEach and ForEach-Object • PowerShell Loops • PowerShell Home
• Conditional Operators • Do While Loop • PowerShell If Statement • PowerShell Brackets
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.