header image

PowerShell while

Posted by: | February 28, 2018 Comments Off on PowerShell while |

PowerShell has a number of looping structures – do; while; for; foreach. This is how the PowerShell while loop works

The while statement has the form:

while (<condition>){<statement list>}


The while loop is probably the simplest of the PowerShell loops. For example:

$x = 0
while ($x -lt 5){
Write-Host “`$x is $x”



$x is 0
$x is 1
$x is 2
$x is 3
$x is 4


As long as the condition is true the statement list is executed

The condition is evaluated BEFORE the loop is executed meaning that it may never run

PS> $x = 10
while ($x -lt 5){
Write-Host “`$x is $x”


The value of $x is greater than 4 so the loop never executes.

under: PowerShell

Comments are closed.