PowerShell Tutorial - Loops
PowerShell Tutorial - Loops
PowerShell
Loops
Introduction #
A loop is a sequence of instruction(s) that is continually repeated until a certain condition is reached. Being able to have your
program repeatedly execute a block of code is one of the most basic but useful tasks in programming. A loop lets you write a
very simple statement to produce a significantly greater result simply by repetition. If the condition has been reached, the next
instruction "falls through" to the next sequential instruction or branches outside the loop.
Syntax
for ( <Initialization>; <Condition>; <Repetition> ) { <Script_Block> }
<Collection>.foreach( { <Script_Block_with_$__as_current_item> } )
Remarks
Foreach
There are multiple ways to run a foreach-loop in PowerShell and they all bring their own advantages and disadvantages:
Foreach Fastest. Works best with static collections (stored in a variable). No pipeline input or output
statement
ForEach() Same scriptblock syntax as Foreach-Object , but faster. Works best with No support for pipeline input.
Method static collections (stored in a variable). Supports pipeline output. Requires PowerShell 4.0 or
greater
Foreach- Supports pipeline input and output. Supports begin and end-scriptblocks Slowest
Object for initialization and closing of connections etc. Most flexible solution.
(cmdlet)
Performance
https://sodocumentation.net/powershell/topic/1067/loops 1/6
14/11/21 09:10 PowerShell Tutorial - Loops
"Foreach: $($foreach.TotalSeconds)"
"ForEach-Object: $($foreachobject.TotalSeconds)"
Example output:
Foreach: 1.9039875
ForEach-Object: 10.7543821
While Foreach-Object is the slowest, it's pipeline-support might be useful as it lets you process items as they arrive (while
reading a file, receiving data etc.). This can be very useful when working with big data and low memory as you don't need to
load all the data to memory before processing.
For
for($i = 0; $i -le 5; $i++){
"$i"
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.
Foreach
ForEach has two different meanings in PowerShell. One is a keyword and the other is an alias for the ForEach-Object cmdlet.
The former is described here.
This example demonstrates printing all items in an array to the console host:
Like the last example, this example, instead, demonstrates creating an array prior to storing the loop:
$Numbers = @()
$Numbers += $Number
While
A while loop will evaluate a condition and if true will perform an action. As long as the condition evaluates to true the action will
continue to be performed.
https://sodocumentation.net/powershell/topic/1067/loops 2/6
14/11/21 09:10 PowerShell Tutorial - Loops
while(condition){
code_block
The following example creates a loop that will count down from 10 to 0
$i = 10
$i
$i--
Unlike the Do -While loop the condition is evaluated prior to the action's first execution. The action will not be performed if the
initial condition evaluates to false.
Note: When evaluating the condition, PowerShell will treat the existence of a return object as true. This can be used in several
ways but below is an example to monitor for a process. This example will spawn a notepad process and then sleep the current
shell as long as that process is running. When you manually close the notepad instance the while condition will fail and the
loop will break.
Start-Process notepad.exe
ForEach-Object
The ForEach-Object cmdlet works similarly to the foreach statement, but takes its input from the pipeline.
Basic usage
$object | ForEach-Object {
code_block
Example:
$names = @("Any","Bob","Celine","David")
$names | ForEach-Object {
Foreach-Object has two default aliases, foreach and % (shorthand syntax). Most common is % because foreach can be
confused with the foreach statement. Examples:
$names | % {
$names | foreach {
Advanced usage
Foreach-Object stands out from the alternative foreach solutions because it's a cmdlet which means it's designed to use the
pipeline. Because of this, it has support for three scriptblocks just like a cmdlet or advanced function:
https://sodocumentation.net/powershell/topic/1067/loops 3/6
14/11/21 09:10 PowerShell Tutorial - Loops
Begin: Executed once before looping through the items that arrive from the pipeline. Usually used to create functions for use
in the loop, creating variables, opening connections (database, web +) etc.
Process: Executed once per item arrived from the pipeline. "Normal" foreach codeblock. This is the default used in the ex-
amples above when the parameter isn't specified.
End: Executed once after processing all items. Usually used to close connections, generate a report etc.
Example:
$results = @()
} -Process {
} -End {
$results
Do
Do-loops are useful when you always want to run a codeblock at least once. A Do-loop will evaluate the condition after execut-
ing the codeblock, unlike a while-loop which does it before executing the codeblock.
Do {
code_block
} while (condition)
Loop until the condition is true, in other words, loop while the condition is false:
Do {
code_block
} until (condition)
Real Examples:
$i = 0
Do {
$i++
"Number $i"
Do {
$i++
"Number $i"
Do-While and Do-Until are antonymous loops. If the code inside the same, the condition will be reversed. The example above
illustrates this behaviour.
ForEach() Method
4.0
Instead of the ForEach-Object cmdlet, the here is also the possibility to use a ForEach method directly on object arrays like so
(1..10).ForEach({$_ * $_})
(1..10).ForEach{$_ * $_}
16
25
36
49
64
81
100
Continue
The Continue operator works in For , ForEach , While and Do loops. It skips the current iteration of the loop, jumping to the
top of the innermost loop.
$i =0
$i++
Write-Host $I
The above will output 1 to 20 to the console but miss out the number 7.
Note: When using a pipeline loop you should use return instead of Continue .
Break
The break operator will exit a program loop immediately. It can be used in For , ForEach , While and Do loops or in a Switch
Statement.
$i = 0
$i++
Write-Host $i
Note: When using a pipeline loop, break will behave as continue . To simulate break in the pipeline loop you need to incorpo-
rate some additional logic, cmdlet, etc. It is easier to stick with non-pipeline loops if you need to use break .
Break Labels
Break can also call a label that was placed in front of the instantiation of a loop:
https://sodocumentation.net/powershell/topic/1067/loops 5/6
14/11/21 09:10 PowerShell Tutorial - Loops
$i = 0
$j = 0
$k = $i*$j
break mainLoop
$j++
$i++
Note: This code will increment $i to 8 and $j to 13 which will cause $k to equal 104 . Since $k exceed 100 , the code will
then break out of both loops.
Previous Next
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC
BY-SA 3.0
S U P P O R T & PA R T N E R S
Advertise with us
Contact us
Privacy Policy
S TAY C O N N E C T E D
https://sodocumentation.net/powershell/topic/1067/loops 6/6