Search This Blog

PowerShell - Loops and Switches

For

Run a command block based on a conditional test.
Syntax
      for (init; condition; repeat) 
        {command_block}

Key

   init      Commands, separated by commas, to run before the loop begins.
             Typically used to initialize a variable with a starting value.

   condition If this evaluates to TRUE the for loop {command_block} runs
             when the loop has run once the condition is evaluated again

   repeat    Commands, separated by commas, to run each time the loop repeats.

A typical use of the for loop is to operate on a subset of the values in an array.
In most cases, if you want to iterate all values in an array, consider using a foreach statement.
Examples
Count to 10:
 PS> for($i=1; $i -le 10; $i++){Write-Host $i}
You can use carriage returns instead of semi-colons:

 PS> for($i=1
     $i -le 10
     $i++){
       Write-Host $i
     }

ForEach (loop statement)

Loop through a set of input objects and perform an operation (execute a block of statements) against each.
Syntax
      ForEach [-Parallel] (item In collection) {ScriptBlock}

key
   item         A variable to hold the current item
   collection   A collection of objects e.g. filenames, registry keys, servernames
   ScriptBlock  A block of script to run against each object.
   -Parallel    Run the commands once for each item in parallel (PowerShell Workflow only).
The collection will be evaluated and stored as an array in memory before the scriptblock is executed.
The foreach statement does not use pipelining (unlike ForEach-Object ) If you use foreach in a command pipeline PowerShell will actually run the foreach alias that calls ForEach-Object.
Use the ForEach statement when the collection of objects is small enough that it can be loaded into memory. Use the ForEach-Object cmdlet when you want to pass only one object at a time through the pipeline, minimising memory usage. In most cases ForEach will run faster than ForEach-Object, there are exceptions, such as starting multiple background jobs. If in doubt test both options with Measure-Command.
ForEach -Parallel is valid only in a PowerShell Workflow, there is currently no general parallel support for the foreach keyword.
In PowerShell 4.0 and later, the ForEach method provides even faster performance.
Examples
Loop through an array of strings:
 $trees = @("Alder","Ash","Birch","Cedar","Chestnut","Elm")  foreach ($tree in $trees) {    "$tree = " + $tree.length  }
Loop through a collection of the numbers, echo each number unless the number is 2:
 foreach ($num in 1,2,3,4,5) {   if ($num -eq 2) { continue } ; $num  } Loop through a collection of .txt files:
  foreach ($file in get-ChildItem *.txt) {     Echo $file.name   }

While

Run a command block based on the results of a conditional test.
Syntax
      while (condition) {command_block}

Key

   condition      If this evaluates to TRUE the loop {command_block} runs.
                  when the loop has run once the condition is evaluated again

   command_block  Commands, separated by commas, to run each time the loop repeats.
As long as the condition remains true, PowerShell reruns the {command_block} section.
Examples
Count to 10:
 PS> while($val -ne 10) { $val++ ; Write-Host $val }
You can use carriage returns instead of semi-colons:
 PS> while($val -ne 10)
     {
       $val++
       Write-Host $val
     }

Do

Run a command block based on the results of a conditional test.
Syntax
      [:Loop_label] Do
      {
         command_block
      } while (condition)

or

      [:Loop_label] Do
      {
         command_block
      } until (condition)

Key

   condition      If this evaluates to TRUE the loop {command_block} runs.
                  when the loop has run once the condition is evaluated again

   command_block  Commands, separated by commas, to run each time the loop repeats.

   :Loop_label    An optional label than can be used to break or continue.
As long as the condition is met, PowerShell will continue to rerun the {command_block} section unless interrupted by a break or continue statement.
Examples
Count to 10:
 PS> Do { $val++ ; Write-Host $val } while($val -ne 10) 
You can use carriage returns instead of semi-colons:
 PS> Do { $val++ 
          Write-Host $val
        } while($val -ne 10) 

No comments:

Post a Comment