A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages
PowerShell loops, at their most basic, simply repeat the same set of commands a set number of times. Ideal for performing consistent actions for a set period of time or a certain number of records, loops can simplify your scripts in a big way. PowerShell in particular features a number of cmdlets -- notably those that begin with the verb Get -- which return objects containing large numbers of similar data.
There are several types of loops available in PowerShell, and in many cases more than one loop technique can be used effectively. At times determining the most efficient loop type is required, either from a performance or code readability perspective.
The while statement runs a statement list zero or more times based on the results of a conditional test.
$i = 0 while($i -lt 3) { Write-Output $i $i++ }
The do while statement runs a statement list one or more times based on the affirmative results of a conditional test.
$i = 0 do { Write-Output $i $i++ }while($i -lt 3)
The do until statement runs a statement list one or more times based on the negative results of a conditional test.
$i = 0 do { Write-Output $i $i++ }until($i -ge 3)
The for statement runs a statement list zero or more times based on an initial setting, a conditional test, and a repeated statement, most often used with numeric counters
for($i = 0; $i -lt 3; $i++) { Write-Output $i }
For loops are often nested to repeat actions, such as for rows and columns. For example:
for($i = 0; $i -lt 3; $i++) { $line = '' for($j = 0; $j -lt 3; $j++) { $line += $i.ToString() + $j.ToString() + ' ' } Write-Output $line }
The foreach statement runs a statement list once for each item in a collection.
$processes = Get-Process foreach($process in $processes) { if($process.PM / 1024 / 1024 -gt 100) { Write-Output ('Process ' + $process.Name + ' is using more than 100 MB RAM.') } }
The continue statement immediately returns script flow to the top of the innermost While, Do, For, or ForEach loop
$processes = Get-Process foreach($process in $processes) { if($process.PM / 1024 / 1024 -le 100) { continue } Write-Output ('Process ' + $process.Name + ' is using more than 100 MB RAM.') }
The break statement causes Windows PowerShell to immediately exit the innermost While, Do, For, or ForEach loop or Switch code block
$processes = Get-Process foreach($process in $processes) { if($process.PM / 1024 / 1024 -gt 100) { Write-Output ('Process ' + $process.Name + ' is using more than 100 MB RAM.') break } }
Exit causes Windows PowerShell to exit a script or a Windows PowerShell instance.
if() { Exit }
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 offers better performance
Get-Process | ForEach-Object { if($_.PM / 1024 / 1024 -gt 100) {'Process ' + $_.Name + ' is using more than 100 MB RAM.'} }
The Start-Sleep cmdlet allows you to pause Windows PowerShell activity for a specified period of time. It is particularly useful with long-running scripts or infinite loops.
while($true) { if((Get-Date -Format 'hh:mm') -eq '00:00') { 'It''s midnight. You should go to sleep.' } Start-Sleep -Seconds 60 }
The Get-Counter cmdlet gets performance counter data from local and remote computers.
Get-Counter