While

While loops can have a simple format.

while($val -ne 3)
{
    $val++
    Write-Host $val
}

Some while loops are a little more complicated. This is a simple menu example from 4sysops.com. It takes user input and carries out actions depending on the user input.

while(($inp = Read-Host -Prompt "Select a command") -ne "Q"){
    switch($inp){
       L {"File will be deleted"}
       A {"File will be displayed"}
       R {"File will be write protected"}
       Q {"End"}
       default {"Invalid entry"}
       }
    }

Last updated