PowerShell - Break out of a loop or block

by
Jeremy Canfield |
Updated: May 09 2023
| PowerShell articles
break can be used to break out of a loop or block. In this example, the script would break in the first if statement so that the second if statement is not invoked.
$foo = "xxx"
$bar = "world"
if ($foo -eq "hello") {
Write-Host "foo equals hello"
}
else {
Write-Host "foo does NOT equal hello - breaking out"
break
}
if ($bar -eq "world") {
Write-Host "bar equals world"
}
else {
Write-Host "bar does NOT equal world - breaking out"
break
}
Did you find this article helpful?
If so, consider buying me a coffee over at