Loops
foreach over a collection
Readable for scripts that walk known lists.
$names = @("Ada", "Sam", "Mina")
foreach ($name in $names) {
Write-Output "Hello, $name"
}
while
Loop while a condition stays true—guard against infinite loops.
$i = 1
while ($i -le 3) {
Write-Output $i
$i++
}
Pipeline ForEach-Object
Fits naturally after Get-* commands.
1..5 | ForEach-Object { $_ * $_ }
Break and continue
break leaves the loop; continue skips to the next iteration.
Comments
One comment per signed-in account. Comments are saved with this page’s URL.