Skip to content
VectorBCO edited this page Jun 29, 2021 · 11 revisions

Goto topics list

Loops

Wiki Loop - is a sequence of instructions executed repeatedly

+ Convenient construction of iterating over objects or properties one by one per iteration
+ Convenient design of repetition of code blocks, functions, etc.

- Resource-intensive functionality
- Easy to make an error with nesting and number of elements that can lead to decreased performance
- Ability to create an eternal loop (script freeze)

General commands help:

Use cases for different loop types

Do { <Code> } Until ( <Statement> )

Until <Statement> will not be $True <Code> will be executed in a loop
For any <Statement>, <Code> will be executed at least once.

Example

$i = 1
Do {
    $i
    $i++
} Until ( $i -ge 5 )

For ( <OnceBeginConfig> ; <Statement> ; <EachIterationCodeBlock> ) { <Code> }

Conventional mechanism allowing to repeat a piece of code several times until <Statement> will be $True
Identical mechanisms exists in all languages.

Example №1 (traditional one)

For ( $i = 1 ; $i -le 5 ; $i++ ) { $i }

Example №2 (alternate one)

For ($i = $k = 1 ; $i -le 5; write-host "k : $k") { 
    $i
    $i++
    $k = $i
}

While ( <Statement> ) { <Code> }

If <Statement> is $True - code will be executed
Unlike Do\Until if <Statement> will be $False code inside the loop will not be executed even once

Example

$i = 1
While($i -le 5){
    $i
    $i++
}

PowerShell 5 Foreach-Object

There are 2 different constructions – Foreach and Foreach-Object, where in some cases Foreach is an alias for Foreach-Object, and in some cases these constructions differ in their principle of operation. In example 1 and example 2, both the Foreach-Object and the Foreach alias can be used. Example 3 Foreach-Object cannot be used.

Example №1 Objects transfer via pipeline

1..5 | Foreach-Object { $_ }

Example №2 Same as Exampleа №1 without pipeline

ForEach-Object -InputObject @(1..5) -Process { $_ }
# Instead $_ could be used $PSItem

Example №3 Iterating over items in a predefined (non-automatic) variable

ForEach($i in @(1..10)) { Test-Connection -Quiet "172.28.110.$i" -Count 1 }

PowerShell 7 Foreach-Object -Parallel

All statements for PowerShell 5 are true except for Workflow, which in version 7 is designated as Deprecated At the same time, Foreach-Object -Parallel in PoSh7 works without additional wrappers (out of box)

Example

1..10 | ForEach-Object -Parallel { Test-Connection -Quiet "172.28.110.$_" -Count 1 }

Control commands

Inside the loop could be used control commands for skipping or ending loop(s)

Continue

Example №1

In this Example in the output will be skipped each combination where j == 7 Same way this will work with do\until, for and while

Foreach($i in @(1..5)) {
    Foreach($j in @(9..5)) {
        if ($j -eq 7){continue}
        Write-Host "i = $i || j = $j"
    }
}

Example №2

In this Example command will work totally different as in Example 1 Code will be ended when j == 7

1..5 | Foreach {
    $i = $_
    9..5 | Foreach {
        $j = $_
        if ($j -eq 7){continue}
        Write-Host "i = $i || j = $j"
    }
}

Break

Example №1

Processing of the nested foreach will be interrupted if j == 7 Same way this will work with do\until, for and while

Foreach($i in @(1..5)) {
    Foreach($j in @(9..5)) {
        if ($j -eq 7){break}
        Write-Host "i = $i || j = $j"
    }
}

# Output:
i = 1 || j = 9
i = 1 || j = 8
i = 2 || j = 9
i = 2 || j = 8
i = 3 || j = 9
i = 3 || j = 8
i = 4 || j = 9
i = 4 || j = 8
i = 5 || j = 9
i = 5 || j = 8

Example №2

Will work similarly to the continue example (Example №2)

1..5 | Foreach {
    $i = $_
    9..5 | Foreach {
        $j = $_
        if ($j -eq 7){break}
        Write-Host "i = $i || j = $j"
    }
}

Example №3

If (j == 7) condition is met, an exit will be made to the tagged point. Thus, several (selected) loops can be exited Same way this will work with do\until, for and while

:quit Foreach($i in @(1..5)) {
    Foreach($j in @(9..5)) {
        if ($j -eq 7){break quit}
        Write-Host "i = $i || j = $j"
    }
}