OSPowerShell.007 introduces iteration: executing a block of instructions repeatedly without copying the same command into a script. PowerShell supplies language loops such as foreach and while, plus the pipeline cmdlet ForEach-Object. Their purposes overlap, but their input handling and control-flow behavior differ. The examples below are read-only or operate on invented data, making them suitable for a first lab.
The chapter follows OSPowerShell.006 on conditionals and OSPowerShell.005 on collections. Readers should understand a variable, a collection, and an if condition before building repeated operations.
The foreach statement: repeat over a collection
Use a foreach statement when the items are available as a collection and a named loop variable makes the script readable. Each iteration assigns the next element to $temperature. Microsoft’s about_Foreach reference documents the language syntax.
$temperatures = @(61, 65, 69)
$total = 0
foreach ($temperature in $temperatures) {
$total += $temperature
}
[math]::Round($total / $temperatures.Count, 1)
The result is 65, the arithmetic mean of three invented temperature readings: (61 + 65 + 69) ÷ 3. The numbers are not real server measurements and carry no implied alarm threshold. Division requires a nonempty collection; production scripts should check the count before calculating an average. OSPowerShell.004 explains the assignment and object values used here.
ForEach-Object: act on pipeline input
ForEach-Object is a cmdlet, not the same syntax as the foreach statement. It can receive objects as they pass through the pipeline. Within its script block, $_ (also named $PSItem) represents the current input object. The example reads process names and identifiers without changing processes:
Get-Process |
Select-Object -First 5 |
ForEach-Object { "$($_.ProcessName) : $($_.Id)" }
The quoted expression produces a string for each object. Actual process names and IDs vary by computer. The official ForEach-Object reference also describes Begin, Process and End script blocks. Review OSPowerShell.003 on object pipelines for the earlier foundations.
While: continue until a condition changes
A while loop evaluates its condition before every iteration. The following bounded example stops after three passes because the counter increases inside the loop:
$attempt = 0
while ($attempt -lt 3) {
$attempt++
"Pass $attempt"
}
The output reads Pass 1, Pass 2 and Pass 3. A condition that never becomes false can create an infinite loop. Microsoft’s about_While reference documents the construct. A for loop is another choice when an index and increment belong naturally in the loop declaration.
Which loop should a technician choose?
Choose foreach for a readable pass through a known set of items, ForEach-Object to transform pipeline results, and while for condition-controlled repetition. Prefer explicit cmdlet names over the % alias in instructional and production scripts. The ForEach-Object -Parallel option requires PowerShell 7 or newer and adds concurrency costs and ordering considerations; it is not a default speed upgrade.
For a practical extension, create five fictional rack identifiers, enumerate them with both foreach forms, and verify that both produce five outputs. Do not substitute live change commands while learning. OSPowerShell.002 shows how to inspect the properties that make such exercises useful.
Lesson conclusion: loops eliminate repetition, but safe automation begins with understanding exactly what enters each iteration, when execution stops, and whether the operation reads or changes a system.

Leave a Reply