PowerShell

PowerShell: Difference between Break, Return and Exit

In this blog post I will show you the difference of three PowerShell statements that are widely used in PowerShell scripts. I am talking about Break, Return and Exit. I will use three examples for explanation. Let’s jump in.

Break

The break statement provides a way to exit the current control block (loop!), Technical reference:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_break?view=powershell-7

What does this mean in a specific case? Let’s take a look at an example. Here it is.

function Test-Break {

    $i = 0
    Clear-Host
    while ($true)
    {
        if (($i+=1) -ge 3)
        {
            break # exits the loop and passes over to line 13 
        }
    Write-Output $i
    }
    Write-Output "Finished testing break." # is displayed - out of loop
}


Note line 9! When you run Test-Break you’ll see that the loop stops at 2 AND that line 13 is executed because the break statement only leaves the loop and passes over to the code outside the loop. Here ist the output:

Well, that brings me to the return statement.

Return

According to the Microsoft reference: Exits the current scope, which can be a function, script, or script block.

Link: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_return?view=powershell-7

Okay, let’s take another example.

function Test-Return {

    Clear-Host
    $i = 0

    while ($true)
    {
        if (($i += 1) -ge 3)
        {
            return # terminates entire function
        }
        Write-Output $i ### is not displayed if $i is 3 or higher
    }
    Write-Output "Finished testing return." # is NOT displayed - out of function
}

In this example, return terminates the current function which means that line 12 and 14 isn’t processed and therefore not displayed. Here is the output:

This brings me to my last statement: Exit

Exit

Exit terminates the current PowerShell session. Note that this will only happen if you execute your script in powershell.exe, pwsh.exe (PowerShell 7) and not in Visual Studio Code.

Here is an example. The function stops at the termination for 2 seconds, so that you can see where it has stopped and terminates PowerShell.

function Test-Exit {

    Clear-Host
    $i = 0

    while ($true)
    {
        if (($i += 1) -ge 3)
        {
            Start-Sleep -Seconds 3
            exit # exits the session (close PowerShell window and related process)
        }
        Write-Output $i
    }
}

If you run Test-Exit you will notice that the loop stops at 2 and then after 2 seconds the PowerShell window will be closed.

Fine, thats it for today.
See you next time.

See also

Categories: PowerShell

Tagged as: ,

2 replies »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.