PowerShell Loops

Loop TypeDescriptionSyntaxExample
For LoopIterates a specified number of times. Useful for iterating over a known range.for (<initialization>; <condition>; <iteration>) { <code> }powershell<br>$sum = 0<br>for ($i = 1; $i -le 5; $i++) {<br> $sum += $i<br>}<br>Write-Output $sum # Outputs: 15
While LoopRepeats a block of code while a specified condition is true.while (<condition>) { <code> }powershell<br>$counter = 1<br>while ($counter -le 5) {<br> Write-Output $counter<br> $counter++<br>}<br>
Do-While LoopSimilar to a while loop, but guarantees that the code block will be executed at least once.do { <code> } while (<condition>)powershell<br>$counter = 1<br>do {<br> Write-Output $counter<br> $counter++<br>} while ($counter -le 5)<br>
Do-Until LoopSimilar to a do-while loop, but continues until the condition becomes true (opposite of do-while).do { <code> } until (<condition>)powershell<br>$counter = 1<br>do {<br> Write-Output $counter<br> $counter++<br>} until ($counter -gt 5)<br>
ForEach LoopIterates over each item in a collection (array, list, etc.).foreach (<item> in <collection>) { <code> }powershell<br>$numbers = @(1, 2, 3, 4, 5)<br>foreach ($num in $numbers) {<br> Write-Output $num<br>}<br>
ForEach-ObjectProcesses each item in the pipeline. Useful for processing elements in the pipeline.“`<collection>ForEach-Object { <code> }“`

PowerShell Advanced Loops

Loop TypeDescriptionSyntaxExample
Break in LoopsExits the loop immediately, regardless of the loop condition.<loop> { if (<condition>) { break } <code> }powershell<br>for ($i = 1; $i -le 10; $i++) {<br> if ($i -eq 5) { break }<br> Write-Output $i<br>}<br>
Continue in LoopsSkips the current iteration and proceeds with the next iteration of the loop.<loop> { if (<condition>) { continue } <code> }powershell<br>for ($i = 1; $i -le 10; $i++) {<br> if ($i -eq 5) { continue }<br> Write-Output $i<br>}<br>
Labelled LoopsAllows the use of labels to control flow when breaking out of nested loops.<label>: <loop> { <inner-loop> { if (<condition>) { break <label> } <code> } }powershell<br>outer: for ($i = 1; $i -le 3; $i++) {<br> for ($j = 1; $j -le 3; $j++) {<br> if ($i -eq 2 -and $j -eq 2) {<br> break outer<br> }<br> Write-Output "i=$i, j=$j"<br> }<br>}<br>
Looping with Pipeline (ForEach-Object)Processes each item in the pipeline using the loop, ideal for handling large datasets.“`<collection>ForEach-Object { <code> }“`
Nested LoopsLoops inside other loops, often used to handle multidimensional arrays or complex iterations.<outer-loop> { <inner-loop> { <code> } }powershell<br>for ($i = 1; $i -le 3; $i++) {<br> for ($j = 1; $j -le 3; $j++) {<br> Write-Output "i=$i, j=$j"<br> }<br>}<br>
Do-While Loop with Condition at EndExecutes the loop body at least once and then repeats until the condition is false.do { <code> } while (<condition>)powershell<br>$i = 1<br>do {<br> Write-Output $i<br> $i++<br>} while ($i -le 5)<br>
Do-Until Loop with Condition at EndExecutes the loop body at least once and then repeats until the condition is true.do { <code> } until (<condition>)powershell<br>$i = 1<br>do {<br> Write-Output $i<br> $i++<br>} until ($i -gt 5)<br>
ForEach Loop with Multiple ActionsIterates over a collection and performs multiple actions within the loop.foreach (<item> in <collection>) { <action1>; <action2>; }powershell<br>$array = @(1, 2, 3)<br>foreach ($item in $array) {<br> $squared = $item * $item<br> Write-Output "$item squared is $squared"<br>}<br>
ForEach-Object with Conditional LogicApplies conditional logic within a pipeline-based loop, filtering or transforming data as it passes through.“`<collection>ForEach-Object { if (<condition>) { <code> } }“`
Using Array Indexes in LoopsAccesses and processes elements of an array by index within a loop.for ($i = 0; $i -lt $array.Length; $i++) { <code> }powershell<br>$array = @(1, 2, 3, 4, 5)<br>for ($i = 0; $i -lt $array.Length; $i++) {<br> Write-Output "Element $i: $($array[$i])"<br>}<br>