in

WindowsWindows Loving ItLoving It

[WORKING] PowerShell Loops QuickSyntax EXAMPLES -TecKangaroo

Working exampls for loops in PowerShell with latest examples. These examples with with quick reference syntax for easy reference learn more!

PowerShell Loops

One of the important jobs for the scripting language is to do repetitive work.  And we use loops for doing such repetitive tasks.  PowerShell has many different looping syntaxes.  Today we are going to see do-while PowerShell.

Jump to:

People also search for:

Do…Until loops in PowerShell:

To demonstrate this we will ask the same question until the user provides the right answer to the question.  We will ask “What is the capital of the USA?” and we will keep asking the same question until the user correctly answers Washington DC.

The following script will do this for us.

$answer = “WashingtonDC”

$useranswer = “no answer”

do{
$useranswer = read-host “What is the capital of the USA?”
if($useranswer -ne $answer)
{
“Wrong answer, try again!”
} }until($useranswer -eq $answer) Write-host “Correct!”

powershell loops
powershell loops

The above script will perform the following actions:

  • Declare a variable named $answer and assign its value as WashingtonDC.  (Note that there is no space or dots in the answer).
  • Declare another variable named $useranswer and assign it no answer. (It is just its initial value)
  • Start the block that is supposed to be carried out, again and again, using the do{ syntax.
  • use the read-host command let to take the user input into $useranswer after writing the text “What is the capital of the USA?”
  • Start the condition check using the if(){} block and see if $useranswer is not equal (-ne) to $answer.
  • If they are not equal, write the text, “Wrong answer, try again!” and close the condition block using the closing curly brace.
  • Finally, we close the loop with the closing curly brace } and place the condition inside the until().  The condition states that the loop shall be iterated again until the user’s answer is the same as the actual answer.  Thus we check if $useranswer is equal to $answer using the equal switch (-eq).
  • Once the answer is the same, the loop stops and we see the text “Correct!”

The above script produces the following output.

Do…While loops in PowerShell:

The logic of Do…While it is the same as that of Do…Until but it will execute only as long as the condition of the loop is true.  That means that unlike the Do…Until, Do…While needs everything to satisfy the condition every time.

The below example will demonstrate Do…While:

$i = 1

$limit = 5

do{
$i
$i = $i +1
}While($i -lt $limit)

The above script will produce the following results.

PowerShell loops
PowerShell loops

The above script will perform the following actions:

  • Declare a variable named $i and set it to 1.
  • Declare a variable named $limit and set it to 5.
  • Start the loop block using the do{.
  • Print the value in $i to the screen.
  • Increase the value in $i by one.
  • Get out of the loop block and check if the value in $i is lower than (lower than is checked with  -lt switch) the value in $limit.

Change the value in $limit and $i simultaneously and see how the loop behaves.  Below are some outputs.

Verdict:

In this post, we learned about some of the most used loop syntaxes in PowerShell.  There are two types of loop shown here, and we recommend you to check it out carefully.  If you have any doubts, please let us know in the comments below.

For more such posts, stay tuned to TecKangaroo.

Leave a Reply

Your email address will not be published. Required fields are marked *