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.

This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s