Bootstrap FreeKB - PowerShell - Break out of a loop or block
PowerShell - Break out of a loop or block

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 289c27 in the box below so that we can be sure you are a human.