Looping through data with While and Do-While

PHOTO EMBED

Wed Dec 28 2022 01:17:32 GMT+0000 (Coordinated Universal Time)

Saved by @savabeh191

# Do While loop
Do {
    Write-Host "Online"
    Start-Sleep 5
}
While (Test-Connection -ComputerName 8.8.8.8 -Quiet -Count 1)

Write-Host "Offline"

# While loop
$i = 0;
$path = "C:\temp"

While ($i -lt 10) {
    # Do Something
    $newFile = "$path\while_test_file_" + $i + ".txt";
    New-Item $newFile
    $i++;
}
content_copyCOPY

Besides ForEach loops, we can also use While and Do-While loops. A while loop will only run when a condition is met, the Do-While always runs once and as long as the condition is true.

https://lazyadmin.nl/powershell/powershell-script/