Learn more with dummies

Enter your email to join our mailing list for FREE content right to your inbox. Easy!

Performing Loops in Windows PowerShell 2

By Steve Seguis

Part of Windows PowerShell 2 For Dummies Cheat Sheet

As you automate your Windows operating system with PowerShell 2, you can automate some of the script you need as well. Loops run the same script block multiple times — often on changing values. A few examples follow:

for ($i = 1; $i -le 5; $i++) { Write-Host $i }
foreach ($i in Get-Alias) { Write-Host $i.name }
$i = 1
while ($i -lt 7) {
     Write-Host $i++
$i = 1
     Write-Host $i++
} while ($i -lt 7)