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

  • Home
  • SEARCH
  • 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 8852587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:26:19+00:00 2026-06-14T13:26:19+00:00

I have a PowerShell script: … Any-Command -ErrorCode Stop … Then I call this

  • 0

I have a PowerShell script:

...
Any-Command -ErrorCode Stop
...

Then I call this script from a bat script:

...
powershell myscript.ps1
...

Now I would like to know in the bat script if the called PowerShell script is stopped on error and not reached the end of the script. How to do it?

  • 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-14T13:26:20+00:00Added an answer on June 14, 2026 at 1:26 pm

    One way to do it would be to add a top level trap statement to your script, something like:

    trap {
       exit 1; # or whatever error code
    }
    

    Note: trap may be considered old fashioned (although I like it top-level), the other option would be a try-finally around your whole script.

    So when Any-Command exits with an exception, the trap statement is executed and the script exits with error code 1. If you don’t add this, Powershell will exit with error code 0 (after all Powershell ran just fine, although the script didn’t).

    You can then detect the error code in a bat script using something like:

    powershell -noprofile -noninteractive -file <<your script>>
    
    IF ERRORLEVEL 1 (echo ERROR) ELSE (echo OK)
    
    pause
    

    This will detect any error code of 1 or higher.

    You can also use %ERRORLEVEL% but be wary that this may be overridden as an environment variable.

    Note: if you’re running your script in a nested runspace, you could consider using $host.SetShouldExit(1) to ensure the root host exits as well.

    • 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 file called ExecutBizTalkAppMSI.ps1 This contains a single function called
I have a powershell script which imports data from a csv file, and then
I have this powershell script named testSwitch.ps1: param( [switch] $s ) Return 's= '
I almost have this Powershell script completed but I am stuck at the last
I have developed a Powershell script that needs to be run automatically daily. This
I have a Batch file which will call a Powershell Script : BATCH FILE
I have a Powershell script that uses this PowerShell module . It basically allows
I have a powershell script that modifies transport rules. It works perfectly from powershell
I have a simple PowerShell script to stop a process: $p = get-process $args
I have this bit of powershell script but i can't get the $DirectoryName to

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.