4

Is there an elegant Powershell script setting that would exit the running Powershell script (or shell instance) if a program fails?

I'm imagining something like Bash feature set -o errexit (or set -e) but for Powershell. In that feature, if a program in the bash script fails (process return code was not 0) then the bash shell instance immediately exits.

In powershell, the script could expliclty check $LastExitCode or $? (if($?){exit}). However, that becomes cumbersome to do for every program call. Maybe powershell has a feature to automatically check and react to program return codes?

To explain in a script

Using made-up idiom Set-Powershell-Auto-Exit

Set-Powershell-Auto-Exit "ProgramReturnCode"  # what should this be?
& "program.exe" --fail  # this program fails with an error code
& "foo.exe"             # this never runs because script has exited
2
  • Can't you place the code in a Try Catch block and then simply exit? Commented Sep 16, 2019 at 0:31
  • @MarkKram a try ... catch would require many lines of code. I was looking for a one-line statement that would set the powershell script mode, similar to bash set -e. Commented Sep 16, 2019 at 1:11

2 Answers 2

10

Unfortunately, as of PowerShell (Core) 7.3.x, PowerShell offers no way to automatically exit a script when an external program reports a nonzero exit code.

For PowerShell-native commands only (cmdlets, scripts, functions), $ErrorActionPreference = 'Stop' can be used, which results in exit code 1.

  • If you need more control over what exit code is reported, see this answer.

  • For an overview of how PowerShell reports exit codes to the outside world, see this answer.

For external programs, you must test for $LASTEXITCODE -eq 0 explicitly and take action based on that, but as a workaround you can use a helper function for all external-program invocations, as shown in this answer.

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be in the latest PowerShell without any flags: github.com/PowerShell/PowerShell-RFC/pull/…
3

As of PowerShell 7.3.0 preview, there is an experimental implementation for exiting scripts when external programs fail. The new RFC PR has moved to another link. As of PowerShell 7.4, the feature is no longer experimental.

# Global config setup for before 7.4.0
Enable-ExperimentalFeature PSNativeCommandErrorActionPreference
exit

Then this can be used in each script:

# Per-script setup
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $true

# Code from original question
& {
    & program.exe --fail  # this program fails with an error code
    & foo.exe             # this never runs because script has exited
}

1 Comment

This seems to be in the latest PowerShell without any flags: github.com/PowerShell/PowerShell-RFC/pull/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.