Run a command block based on a conditional test.

      for (init; condition; repeat) 


   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.


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
       Write-Host $i

“An essential aspect of creativity is not being afraid to fail” ~ Edwin Land

Related PowerShell Cmdlets

Break statement
Continue statement
Comparison operators -like, -lt, -gt, -eq, -ne, -match
ForEach - Loop through values in the pipeline.
IF - Conditionally perform a command.
While Loop while a condition is True.

Copyright © 1999-2023
Some rights reserved