PowerShell : Loops

Like other programming languages, PowerShell supports following types of loop to handle looping requirements.

  • for
  • forEach
  • foreach-object
  • while
  • do-while
  • do-until

For

The for statement runs a statement list zero or more times based on an initial setting, a conditional test, and a repeated statement.

For statement syntax:

for (; ; ) 
{}
for ($i = 0; $i -ile 10; $i++) 
{
    Write-Host $i
}
for (;;)
{
    $i++; 
    Write-Host $i
}

Until you break out of the command by pressing CTRL+C, this statement will continually display the value of the $i variable as it is incremented by 1 each time the loop is run.

ForEach

The foreach statement runs a statement list once for each item in a collection.

$num = 1
$colors = "Red","Blue","Yellow","Green"
foreach ($clr in $colors )
{
   Write-Host "Color $num : $clr"
   $num++
}

Output:

Color 1 : Red
Color 2 : Blue
Color 3 : Yellow
Color 4 : Green

Get the file names greater than 100KB in the current folder and display length and last access time of the file.

foreach ($file in Get-ChildItem) 
{ 
    if ($file.length -gt 100KB) 
    { 
       Write-Host $file 
       Write-Host $file.length 
       Write-Host $file.lastaccesstime 
    } 
}

While

The while statement runs a statement list zero or more times based on the results of a conditional test.

While statement syntax:

while ()
{}
while($val -ne 3)
{
   $val++
   Write-Host $val
}

Output:

1
2
3

Here, while statement initialized $val with 0 and incremented by 1using the ++ unary increment operator. When $val = 3, $val – not equal 3 condition evaluates to false  and the loop exits.

Do While

The do while statement runs a statement list one or more times based on the results of a conditional test.

The syntax of the Do-While statement:

do {
    
} 
while ()
$var = 10
do {
    Write-Host Value : $var
    $var++
}
while ($var -le 15)

Do Until

The do until statement runs a statement list one or more times based on the negative results of a conditional test.

The syntax of the Do-Until statement:

do {

} until ()
$array = 1,2,78,0
$idx = 0
do { 
    Write-Host Values : $array[$idx]
    $idx++; 
} until ($array[$idx] -eq 0)

Output:

Values : 1
Values : 2
Values : 78

Foreach-Object

The ForEach-Object cmdlet runs a command list once for each item in a collection.

While the ForEach-Object cmdlet is often more convenient, the foreach statement and a coded loop usually offer better performance.

Get value of the ProcessName property of each process on the computer.

PS C:\> Get-Process | ForEach-Object ProcessName

Display each value of the collection

PS C:\> "Hello",1,2000,1.5005 | ForEach-Object {Write-Host $_}
Hello
1
2000
1.5005

The $_ variable to represent the current object.

Get the length of all the files in a directory

PS C:\> Get-ChildItem $HOME | ForEach-Object -Process { $_.Name; $_.Length / 1024}

 

Break

The break statement exit the innermost While, Do, For, or ForEach loop or Switch code block.

Continue

The continue statement immediately returns script flow to the top of the innermost While, Do, For, or ForEach loop.

Exit

Exit causes to exit a script or a Windows PowerShell instance.

Start-Sleep

The Start-Sleep cmdlet allows you to pause for a specified period of time.

Get-Counter

The Get-Counter cmdlet gets performance counter data from local and remote computers.