Best way to exit a script on a specific condition.

Welcome Forums General PowerShell Q&A Best way to exit a script on a specific condition.

This topic contains 6 replies, has 6 voices, and was last updated by

9 months, 1 week ago.

  • Author
  • #124995

    Topics: 1
    Replies: 1
    Points: 37
    Rank: Member

    I have a small script I'm working on to check for locked AD accounts, and then optionally unlock them. One of the first checks is to see how many accounts are currently locked. If there are zero locked accounts I want the script to terminate. I've seen a few posts arguing the merits of Exit/Return/Break, and I'm wondering what the best way to go would be. Thoughts? Suggestions?

    If ($NumberOfAccounts -eq 0)
        Write-Host ("There are currently no accounts locked") -ForegroundColor Green
        # Exit code??
  • #125006

    Senior Moderator
    Topics: 8
    Replies: 1009
    Points: 3,281
    Helping Hand
    Rank: Community Hero

    No matter what you use, if your intent is to exit the console, then the message in green color wouldn't be useful.

    btwn, I prefer exit...

  • #125007

    Topics: 2
    Replies: 466
    Points: 1,058
    Helping Hand
    Rank: Community Hero

    exit $code is the most recognisable and typically useful way to report an actual exit code. You can do similar things with the methods buried a couple levels deep in the $host automatic variable, but it's easiest to use the keyword. 🙂

  • #125016

    Topics: 1
    Replies: 1
    Points: 37
    Rank: Member

    I'm just looking for a way to exit from the script, not the console. I thought about wrapping it in a huge if...else statement, but that seemed inefficient.

  • #125052

    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

    Why not just use break when you want to leave?

    1..9 | %{
        If ($_ -ne 7){"Not 7 yet, so no break $_"}
        Else {
            Write-Warning -Message "Hello, I hit $_ breaking..."
            Write-Host 'New line after break ' -ForegroundColor Cyan
  • #125115

    Topics: 12
    Replies: 231
    Points: 461
    Helping Hand
    Rank: Contributor

    I would say it depends on what you want to do.

    But it depends on which context you're in.
    Break and return will produce the same result if you're in the same scope.
    E.g. lets say you have a script and not using functions in the script both return and break will stop executing the script.

    If you are executing some code in a function in that script.
    Return will exit the function but will continue with the script.
    Break will stop execution of the rest of the script.

    Exit will exit the script and close the console.

    In regard to Exit it depends on what you want to achieve afterwards.
    Exit 0 will return the exit/errorcode of 0.
    Exit 'any none zero' value will produce the exit/errorcode of 1.

    So if you want more granular error codes than 0 or 1.
    Use: [System.Environment]::Exit(any_number) or $host.SetShouldExit(any_number).

    Haven't explored ways to capture the error code other than running the script from a normal command prompt.
    Since the console will exit once it hits one of the exit variants.
    But I'm sure you can do it somehow, maybe launching the script in a new powershell session might work.
    Haven't tested it though.

  • #125376

    Topics: 0
    Replies: 2
    Points: -11
    Rank: Member

    The usage of Break, Exit, and return depending on which context you are trying to use that.

    Consider a situation where you only want to come out form a loop or a if block you can simply use a break. It won't terminate the whole script.

    and if you want to terminate the whole Script you have to use Exit in that case.

    and the return we mostly use with function. return will terminate the function and send the control to the line where you called the function from.

The topic ‘Best way to exit a script on a specific condition.’ is closed to new replies.