Close

PowerShell – exiting out of outerloop

Special Chapter – exiting out of outerloop

Announcement

I have released my new course on Udemy, Kubernetes By Example. Sign up now to get free lifetime access!

when exiting out of an outer loop, you are effecting exiting out from all the inner loops.

You do this by labeling your loops like this:

$list = 1,2,3,4,5

# here we use the “:{loops label}” syntax to give a loop a name, then refer to it when we do continue/break
:outerloop foreach ($i in $list) {
for ($j = 0; $j -lt 5; $j++) {
if ($j -eq 2) {
continue outerloop # notice here that we are specifying which loop to apply continue to.
}
Write-Host “outer loop counter = $i, inner loop counter = $j”
}
}

For more info, see:

http://www.out-web.net/?p=785