PowerShell Loops

PowerShell Loops

Each programming language or scripting language has several different methods of applying the loop. Looping is a basic programming process that allows us to repeat a command and process large amounts of data. Without this ability, writing scripts would be tedious and almost impossible.

Windows PowerShell supports the following loops:

  • foreach – Executes script block for each item in a collection or array.
  • for – Script block executes a specified number of times.
  • do while  Script block executes as long as condition is True.
  • do until – Script block executes until the condition is True.

ForEach Loop

The simplest and most useful loop is foreach. This is the default alias for ForEach-Object cmdlet. The ForEach loop performs an operation for each item in a collection or array.

The syntax of ForEach loop is shown below:

foreach ($<item> in $<collection>){<statement list>}

The following commands create a new $arr array and then ForEach loop displays the each item of $arr array. Basically the foreach loop creates a temporary variable $a for every object of $arr array each time the loop executes.

PS D:\MyScripts> $arr = 100,200,300,400,500
PS D:\MyScripts> ForEach ($a in $arr) {$a}
100
200
300
400
500

The value of every other object  keeps over writing every time the loop executes. So, at the end of loop, the temporary variable $a contains the value of last object. If you look at the content of $a after the loop is completed, you will see value 500 since it is the last member of $arr array.

PS D:\MyScripts> $a
500

ForEach loop is one of the most widely used PowerShell loop in scripts.

For Loop

For loop is standard loop which is used to execute a code specific number of times.

The syntax of For loop is shown below:

for (<init>; <condition>; <repeat>)
{<statement list>}

The <init> represents one or more commands, separated by commas, that are run before the loop begins. You typically use the <init> portion of the statement to create and initialize a variable with starting value.

The <condition> represents the portion of the For statement that resolves to a true or false Boolean value. Windows PowerShell evaluates the condition each time the For loop runs. If the statement is true, the commands in the command block run, and the statement is evaluated again. If the condition is still true, the commands in the statement list run again. The loop is repeated until the condition
becomes false.

The <repeat> represents one or more commands, separated by commas, that are executed each time the loop repeats. Typically, this is used to modify a variable that is tested inside the <condition> part of the statement.

The <statement list> represents a set of one or more commands that are run each time the loop is entered or repeated. The contents of the statement list are surrounded by braces.

For example, the following command initializes a $i variable with a value 1 and then the test condition is set if the value of $i is less than 5, the value of $i is displayed on screen. The value inside $i is incremented by 1 each time for loop executes. Once the value of $i becomes 5, the test condition evaluates to false thus the For loop exits.

PS D:\MyScripts> For ($i=1; $i -lt 5; $i++)  {Write-Host $i}
1
2
3
4

So the value of $i is displayed in console until it is less than 5.

While Loop

The While loop runs commands in a command block as long as a conditional test evaluates to true. The While statement is easier to construct than a For statement because its syntax is less complicated.
In addition, it is more flexible than the ForEach loop because you specify a conditional test in the While statement to control how many times the loop runs.

The following line shows the While loop syntax:

while (<condition>){<statement list>}

When you run a While statement, Windows PowerShell evaluates the <condition> section of the loop before entering the <statement list> section. The condition portion of the statement resolves to either true or false. As long as the condition remains true, Windows PowerShell re-runs the <statement list> section.

The <statement list> section of the loop contains one or more commands that are run each time the loop is entered or repeated.

For example, the following While statement displays the numbers 1 through 5 if the $i variable has not been created or if the $i variable  has been created and initialized to 0.

PS D:\MyScripts> while($i -ne 5)
>> {
>> $i++
>> Write-Host $i
>> }
>>
1
2
3
4
5

Do-While and Do-Until Loop

The Do keyword works with the While keyword or the Until keyword to run the statements in a script block, subject to a condition. Unlike the related While loop, the script block in a Do loop always runs at least once.

A Do-While loop is a variety of the While loop. In a Do-While loop, the condition is evaluated after the script block has run. As in a While loop, the script block is repeated as long as the condition evaluates to true.

Like a Do-While loop, a Do-Until loop always runs at least once before the condition is evaluated. However, the script block runs only while the condition is false.

The following line shows the syntax of the Do-While loop:

do {<statement list>} while (<condition>)

The following line shows the syntax of the Do-Until loop:

do {<statement list>} until (<condition>)

The <statment list> contains one or more statements that run each time the loop is entered or repeated.

The <condition> portion of the statement resolves to true or false.

For example,

PS D:\MyScripts> $i = 1
PS D:\MyScripts> do {Write-Host $i; $i++}
>> while ($i -lt 5)
>>
1
2
3
4

Do-While loop executes the code block as long as condition is true but the code block is always executed at least once before testing the condition.

Do-until loop works almost the same like do-while but is executes the code block until the condition is not true.

PS D:\MyScripts> $i = 1
PS D:\MyScripts> do {Write-Host $i; $i++}
>> until ($i -gt 5)
>>
1
2
3
4
5

As soon as the $i becomes greater than 5, condition becomes true and do until loop exits.

Back



Microsoft Certified Professional | Cisco Certified Network Associate

Leave a Reply