Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8220621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:35:21+00:00 2026-06-07T13:35:21+00:00

How can I make Powershell behave like Bash with its flag set -e ?

  • 0

How can I make Powershell behave like Bash with its flag set -e? set -o errexit makes a Bash script “Exit immediately if a simple command exits with a non-zero status”.

I thought I could do this by setting $ErrorActionPreference="Stop" but this doesn’t seem to work. Suppose I have a script a.ps1

$ErrorActionPreference="Stop"
& cmd /c "exit 1"
echo "cmd exited `$LastExitCode=$LastExitCode and `$?=$?"

If I run it

.\a. ; echo "a.ps1 exited `$LastExitCode=$LastExitCode `$?=$?"

To my surprises it prints

cmd exited $LastExitCode=1 and $?=False
a.ps1 exited $LastExitCode=1 $?=True

What’s going on?! I expected a.ps1 to exit after the first line, throwing an error and setting $? to False.

Is there any official documentation explaining $ErrorActionPreference? All I found was this post on a blog about coffee.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:35:23+00:00Added an answer on June 7, 2026 at 1:35 pm

    $ErrorActionPreference works as intended, it’s just that exit codes from native programs are not nearly as well-behaved as PowerShell cmdlets.

    For a cmdlet the error condition is fairly easy to determine: Did an exception happen or not? So the following would not reach the Write-Host statement with $ErrorActionPreference set to Stop:

    Write-Error blah
    

    or

    Get-ChildItem somepathwhichsurelydoesntexisthere
    

    For native programs an exit code of zero usually signals that no error occurred but that’s merely a convention, as is a non-zero exit code signalling an error. Would you say that if choice exists with an exit code of 1 that it was an error?

    The only robust way to handle this is to check the exit code after running a native command and handling it yourself, if needed. PowerShell doesn’t try to guess what the result meant because the conventions aren’t strong enough to warrant a default behaviour in this case.

    You can use a function if you want to make your life easier:

    function Invoke-NativeCommand {
      $command = $args[0]
      $arguments = $args[1..($args.Length)]
      & $command @arguments
      if ($LastExitCode -ne 0) {
        Write-Error "Exit code $LastExitCode while running $command $arguments"
      }
    }
    

    But in general many programs need different handling because they don’t adhere to said convention.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PowerShell script for building my project files, and I'd like to
I'd like to use a HashSet in a powershell script. I think I've figured
i Have an arguments like the one below which i pass to powershell script
PowerShell can call commandline batch files. PowerShell script output can be recorded with the
Using either Powershell or VBS, how can I make all of the users within
I want to make a PowerShell script that takes each file of my music
I'm trying to start a PowerShell script from python like this: psxmlgen = subprocess.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
In Bash, we can combine two shell commands cd and ls like this: function
I can make my program write a .dat file with an array of Node
How can make a link within a facebox window that redirects it to another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.