At first glance, as in the below shared PowerShell script looks like a simple set of nested foreach loops printing numbers. But it has very powerful - and always overlooked - control structure: called Loop Label.
:OuterLoop foreach ($OuterNumber in 1..4) { Write-Host "== Starting Outer Loop: $OuterNumber ==" -BackgroundColor DarkCyan -ForegroundColor White -NoNewline; Write-Host ([char]0xA0) :MiddleLoop foreach ($MiddleNumber in 1..4) { Write-Host " -- Middle Loop: $MiddleNumber --" -BackgroundColor Magenta -ForegroundColor Black -NoNewline; Write-Host ([char]0xA0) if ($MiddleNumber -eq 3) { Write-Host " Found Middle = 3. Skipping to next Outer Loop..." -ForegroundColor DarkRed -BackgroundColor Black -NoNewline; Write-Host ([char]0xA0) continue OuterLoop # This jumps out of Inner + Middle and goes to next $OuterNumber } :InnerLoop foreach ($InnerNumber in 1..4) { if ($InnerNumber -eq 4) { Write-Host " Inner Loop: $InnerNumber" #break OuterLoop # break InnerLoop #Break the script at the defined label } else { Write-Host " Inner Loop: $InnerNumber" -NoNewline } } } Write-Host "Finished Outer Loop: $OuterNumber`n" # This line never runs because of the continue }
What is the :OuterLoop, :MiddleLoop, and :InnerLoop syntax called as mentioned in the script?
In the PowerShell it is called a Label (or Statement Label). It’s a user-defined identifier followed by a colon (:) that you place before a statement block (like a loop foreach, while). It doesn't change the loop's behavior on its own, but it acts as a named pointer. You can then reference this label inside break or continue statements to tell the script exactly which nested loop you want to exit or restart.
What is this script actually doing?
The script creates three tiers of loops (Outer → Middle → Inner), each iterating from 1 to 4. However, its primary purpose is to demonstrate an emergency exit—specifically, skipping the rest of the current Outer iteration whenever the Middle loop hits the number 3.
Here is a step-by-step breakdown:
- The Outer Loop starts ($OuterNumber = 1). It prints the header.
- The Middle Loop begins. It prints its own index ($MiddleNumber).
- The Critical Check: When $MiddleNumber equals 3, the script hits the continue OuterLoop command.
- The Jump: Instead of just skipping to the next Middle iteration, continue OuterLoop forces PowerShell to look for the label :OuterLoop and immediately jump to the next iteration of that outermost loop, completely aborting the current Middle loop and the remaining Inner loop.
- The Unreachable Code: Because we always jump out when $MiddleNumber reaches 3, the Write-Host "Finished Outer Loop: $OuterNumber" line at the very bottom never runs. The loop skips it every single time.
The Golden Rule of Loop Labels in PowerShell
- break Label exits the labeled loop entirely.
- continue Label skips the remaining code inside that labeled loop's current iteration and moves to the next iteration of that labeled loop.
In your script, using continue OuterLoop is a drastic measure—it bypasses not only the current Inner and Middle loops but also skips the "Finished Outer Loop" message. If you want to avoid dead code, you would typically place such a continue inside a conditional block that logically prevents the rest of the outer loop from executing, making the final Write-Host unreachable by design—or you move that final message to the top of the loop.
Labels are a lifesaver when you are three layers deep in nested loops and need to break out of all of them cleanly, without resorting to messy boolean flags. Just remember: with great power comes great responsibility—use them sparingly, as overusing jumps can make your code harder to read than a tangled maze.
Useful Articles
PowerShell File Renaming: How to Reorder Files Sequentially Using a CSV Map
Building a Unified Ping and Port Connectivity Report with PowerShell
High-Performance Network Auditing: Custom Ping and TCP Port Testing in PowerShell .Net Objects
Parallel Ping and Port Testing in PowerShell Using Runspaces
Get PowerShell Cmdlet Parameter Enum Values Easily
How to Run PowerShell Commands on Azure VMs Remotely
A Step-by-Step Guide to Using Enter-AzVM for Remote Administration Over SSH
How to Audit HPE iLO Active Directory Settings with PowerShell and Redfish API
How to Get Installed Software Features List Using PowerShell WMI-CIM
PowerShell Pro Tip: How to Overwrite and Update the Same Line ProgressBar
How to Capture PowerShell Trace and Debug Messages in DebugView

