Looping statements in PowerShell allow you to execute groups of statements multiple times.
<: loop_label> for(<initialization>; <condition>; <increment>) { <statement block>}
When PowerShell executes a for
statement, it first executes the
expression given by <
initialization
>
—for example, $myCounter = 0
. It next evaluates <
condition
>
—for example, $myCounter –lt 10
. If <
condition
>
evaluates to $true
, PowerShell executes the given
statement block. It then executes the expression given by <
increment
>
—for example, $myCounter++
. PowerShell continues to
execute the statement block and <
increment
>
statement as long as <
condition
>
evaluates to $true
.
The break
and continue
statements (discussed below) may
specify the loop_label
of any enclosing
looping statement as their target.
<: loop_label> foreach(<variable> in <expression>) { <statement block> }
When PowerShell executes a foreach
statement, it executes the pipeline
given by <
expression
>
—for example, Get-Process | Sort Handles
or 1..10
. For each item produced by the
expression, it assigns that item to the variable specified by <
variable
>
and then executes the given statement
block. For example:
$handleSum = 0; foreach($process in Get-Process | sort Handles) { $SCRIPT:handleSum += $process.Handles } $handleSum
The break
and continue
statements (discussed below) may
specify the loop_label
of any enclosing
looping statement as their target.
When PowerShell executes a while
statement, it first evaluates <
condition
>
– for example, $myCounter –lt 10
. If <
condition
>
evaluates to $true
, PowerShell executes the given
statement block. PowerShell continues to execute the statement block
as long as <
condition
>
evaluates to $true
. For example:
The break
and continue
statements (discussed below) may
specify the loop_label
of any enclosing
looping statement as their target.
or
When PowerShell executes a do...while
or do...until
statement, it first executes the
given statement block. In a do...while
statement, PowerShell continues
to execute the statement block as long as <
condition
>
evaluates to $true
. In a do...until
statement, PowerShell continues
to execute the statement as long as <condition>
evaluates to $false
. For example:
$validResponses = "Yes","No" $response = "" do { $SCRIPT:response = read-host "Yes or No?" } while($validResponses -notcontains $response) "Got it." $response = "" do { $SCRIPT:response = read-host "Yes or No?" } until($validResponses -contains $response) "Got it."
The break
and continue
statements (discussed below) may
specify the loop_label
of any enclosing
looping statement as their target.
PowerShell supports two statements to help you control flow
within loops: break
and continue
.
The break
statement halts
execution of the current loop. PowerShell then resumes execution at
the end of the current looping statement, as though the looping
statement had completed naturally. If you specify a label with the
break
statement—for example,
break OUTER_LOOP
—PowerShell halts
the execution of that loop instead.
For example:
Produces the output:
The continue
statement
skips execution of the rest of the current statement block.
PowerShell then continues with the next iteration of the current
looping statement, as though the statement block had completed
naturally. If you specify a label with the continue statement—for
example, continue
OUTER_LOOP
—PowerShell continues with the next iteration of
that loop instead.
For example:
Produces the output:
18.220.64.128