Looping Statements

Looping statements in PowerShell allow you to execute groups of statements multiple times.

for Statement

<: 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.

foreach Statement

<: 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.

while Statement

<: loop_label> while(<condition>)
{
   <statement block>
}

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:

$command = "";
while($command -notmatch "quit")
{
   $command = read-host "Enter your command"
}

The break and continue statements (discussed below) may specify the loop_label of any enclosing looping statement as their target.

do ... while Statement/do ... until Statement

<: loop_label> do
{
   <statement block>
} while(<condition>)

or

<: loop_label> do
{
   <statement block>
} until(<condition>)

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.

Flow Control Statements

PowerShell supports two statements to help you control flow within loops: break and continue.

break

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:

foreach($x in 1..5)
{
   if($x % 2 -eq 0) { break; }
   "Got here $x"
}

Produces the output:

Got here 1

continue

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:

foreach($x in 1..5)
{
   if($x % 2 -eq 0) { continue; }
   "Got here $x"
}

Produces the output:

Got here 1
Got here 3
Got here 5
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.64.128