Repository navigation
Description
Summary of the new feature / enhancement
At first I saw the (labeled) break definition:
PowerShell does not limit how far labels can resume execution. The label can even pass control across script and function call boundaries.
as a convenient feature (INABIAF) to break out of the entire script (even from within a function) and keep the host alive.
But now I think it qualifies as a bug and should actually result in a (parser) error as it concerns a child function/script that potentially (accidentally) terminates the parent function/script.
Note that this also applies to break statements without labels, see e.g.: how to prevent external script from terminating your script with break statement)
A common use case where this might result is an cumbersome trouble shooting situation, is with testing where code misstakes (as label typos) should be captured rather than completely breaking the Pester script omitting any errors or results, see: pester/Pester#2669.
Proposed technical implementation details (optional)
Changing this behavior by default would result in a break change, but a kind of but e.g. a $UnhandledBreakUseErrorActionPreference variable (possibly with value as function-scope, script-scope and host-scope) might be helpful for testing and improving code quality. At a second thought, a preference variable is probably not the best solution as preference variables usually inherit their default setting from the parent script/function.TrapBreak function or trap break statement could be helpful to prevent (unintended) global breaks.
Wishful thinking:
# badscript.ps1
if((Get-Date).DayOfWeek -ne "Yesterday"){
Write-Warning "Sorry, you can only run this script yesterday."
break
}function TrapBreakTest {
trap break { 'unhandled break found.' }
badscript.ps1
}
Activity
237dmitry commentedon Dec 8, 2025
What's wrong with
return,exit(and loop'sbreak)?iRon7 commentedon Dec 8, 2025
Nothing.
What is wrong (imho), is that a break to a non-existing label doesn't produce an error but silently breaks all the parent scripts (including scripts as e.g. Pester).
[-]Unhandled `break` preference[/-][+]Trap unhandled `break` statements[/+]jhoneill commentedon Dec 9, 2025
This should probably be extended to cover
continueFew people expect this to exit a script which calls test-1
running test.ps1 outputs
boo
1-1
2-1
SeeminglyScience commentedon Dec 14, 2025
More specifically it was likely implemented for
ForEach-Object, at least that's how it's most likely to be used today.Also too many people use it today as a way to exit early. Even if incredibly ill-advised, it will technically work for a "controller style script". Making it a parse error would be a massive breaking change.
An option at the host level to write an error when the REPL receives a break or continue exception could be a good idea, though every host that implements its own REPL would need to support the feature individually.
SeeminglyScience commentedon Jan 26, 2026
The Engine WG discussed this today, while we agree the scenario is hard to troubleshoot, we do not feel that adding this functionality to
trapis ideal. We would suggest using the following code as a workaround in the rare case you need to make this determination.For troubleshooting you can also use
Set-PSDebug -Trace 2when attempting to diagnosis the issue.iRon7 commentedon Jan 27, 2026
Patrick Meinecke (@SeeminglyScience),
Thank you for your response but although the workaround works for a simple
break(orcontinue) like:It doesn't work for a
break(orcontinue) for a misspelled (non-existing) label, like:Anyways,
Set-PSDebug -Trace 2certainly helps debugging this but is not something I would expect to be required while running Pester, see: pester/Pester#2669SeeminglyScience commentedon Jan 27, 2026
True! It does not account for that, here's a better way:
microsoft-github-policy-service commentedon Jan 28, 2026
This issue has been marked as declined and has not had any activity for 1 day. It has been closed for housekeeping purposes.