How to use the While loop statement in PowerShell?
In PowerShell, the While loop executes the command in the command block until the user-defined condition remains true. For an overview, head over to the syntax of the while loop.
Syntax
The syntax is described as
- <condition>: it represents the condition set for the while loop
- <statement list>: this represents the lines of code to be executed inside the while loop
The following scripts will give a detailed overview about the use of the While loop.
Example 1: Basic use of While loop
The below PowerShell script is used to run the while loop till the condition is True.
{
$box ++ #increment with 1
Write-Host $box #print the loop
}
The syntax is described as
- <condition>: it represents the condition set for the while loop
- <statement list>: this represents the lines of code to be executed inside the while loop
The following scripts will give a detailed overview about the use of the While loop.
Example 1: Basic use of While loop
The below PowerShell script is used to run the while loop till the condition is True.
{
$box ++ #increment with 1
Write-Host $box #print the loop
}
The code can be described as,
- the condition for the while loop is “$box -lt 10”, which states that the loop will keep on executing the lines of code until the $box value reaches 10.
- on each iteration, the value of the $box is incremented by 1.
The above output states that the While loop returned the list of numbers from 1 to 10.
Here, we have explained how to print a sequence of numbers with a gap of a specific value (incrementing the value by 5).
{
$box += 5 #increment with 5
Write-Host $box #print the loop
}
In the above code:
- the while loop’s condition is defined with “$box -lt 50” which indicates the loop will continue to execute until the $box value reaches 50.
- the value of the $box is incremented by 5 on each iteration.
The above screenshot shows that a sequence of numbers is printed but with an increment of 5.
Example 2: How to break a sequence at a specific value?
Suppose, you want to break the loop process at a specific iteration/step. For this, you have to utilize the if statement and a break keyword. The example code is written below with predefined conditions.
{
$box ++ #increment with 1
if ($box -eq 5) #if the variable equals to 5
{
break #break the loop
}
Write-Host -Object $box #print the loop
}
In the example code:
- the condition “$box -lt 10” is specified for the while loop.
- $box values are updated with the increment value 1 on each iteration.
- within the While loop, the “if” condition is defined as “$box -eq 5” which will break the loop when it reaches 5.
The loop iterated until it found the break inside the if statement. When the $box value reaches 5 the loop is terminated instantly and the values(1,2,3,4,5) are printed as can be observed in the output.
Example 3: How to Skip a value in a sequence?
You can skip a value in sequence by using the “Continue” keyword. The example script is as follows to demonstrate the functionality of “Continue”.
{
$box ++ #increment with 1
if ($box -eq 5) #if the variable equals to 5
{
continue #continue the loop
}
Write-Host $box #print the loop
}
Here the code do the followings:
- the condition for the While loop is set with “$box -lt 10”. It will get executed 10 times.
- with the $box ++ command the value of $box is incremented by 1
- the “if” condition “$box -eq 5” will be checked on each iteration
- when the value of the loop variable reaches 5, the body of the if-statement is executed.
According to the above output, when “$box” equals to “5”, the given value (5) will be skipped due to the continue statement and the rest of the loop keeps on working until the value reaches 10.
Example 4: How to Create a Loop on a Text File?
The text file can also be iterated with a loop statement. The given-below code will explain how the While loop works on text files.
$testfile = Get-Content C:\Users\powershell\Documents\test\testfile.txt
While ( $i -le $testfile.length )
{
$testfile[$i]
$i++
}
In this script the following tasks are performing:
- initialized the value (i.e., $i=0)
- The content of the file “testfile.txt” is stored in a $testfile variable
- In while loop, “$i -le $testfile.length” condition is called to iterate each line based on the length of the $testfile
- the While loop body “$testfile[$i]” is executed until it reaches the length of the $testfile.
It can be seen that the While loop statement iterates line-by-line and prints all the lines against the testfile.txt length.
Example 5: How to print the items of a list?
The following script is about the use of the While loop statement on the elements of an array.
$i = 0 #initiate from zero
while ($i -lt $items.count) #count the list items
{
Write-Host $items[$i++] #display the items one-by-one
}
You can see in the script that we have:
- created a list ($items)
- initialized the loop variable (i.e., $i=0)
- the condition of the while loop is set to ($i -lt $items.count)
- Inside the loop, the items are printed one by one
As per the screenshot attached above, the While loop iterates on each item of the list and prints all the items on the PowerShell console.
Well done! You have done your job to learn more about the PowerShell While loop.
Conclusion
The While loop can be used to iterate/repeat a sequence of statements. With a simple syntax structure, the while loop statement works in a less complicated way. While loop statement executes the code in the command block until the condition is satisfied. In this article, you have learned that the while loop can be used on numbers, text files, and arrays/tuples in PowerShell.