I\'ve been looking for a way to terminate a PowerShell (PS1) script when an unrecoverable error occurs within a function. For example:
function foo() {
#
I coincidentally found out that Break
(e.g. simply Break Script
, where the label Script
doesn't exists) appears to break out of the entire script (even from within a function) and keeps the host alive.
This way you could create a function that breaks the script from anywhere (e.g. a recursive loop) without knowing the current scope (and creating labels):
Function Quit($Text) {
Write-Host "Quiting because: " $Text
Break Script
}