1

How do I negate the exit code of an executable in powershell? I would like my script to succeed if an executable terminated with a non-zero exit code, or fail if the executable terminated with a zero exit code.

I tried the following ! (...) and it prints True, which sounds promising, however it also makes the script fail with exit code 1.

! (program.exe) # I want this line to fail if program.exe returns 0

Output:

True
Error: Process completed with exit code 1.

Note that I'm just interested in checking the exit code of the executable. If the executable fails for other reasons (e.g. missing libraries) then it's fine for the script to fail.

This question has some similarities to this question for -nix systems. However, I'm writing a powershell script to be run on Windows 10.

9
  • include the full command please Commented Oct 12, 2020 at 16:07
  • @marsze The command that I want to run is an executable, and I'm looking for a solution that works for any kind of executable. Commented Oct 12, 2020 at 16:13
  • 2
    @Federico - since there is no way to predict what any given exe will do when it fails ... how on earth is one supposed to make a universal test? you MUST know what to look for ... and that is not always the same for any given application or utility. Commented Oct 12, 2020 at 16:22
  • @Lee_Dailey You can do it on *nix platforms: stackoverflow.com/questions/367069/…. I need the analogous, for powershell. Commented Oct 12, 2020 at 16:25
  • 1
    @Federico - try checking the $LastExitCode entry in the help file about_Automatic_Variables. i don't know if it works for the -nix stuff, tho. Commented Oct 12, 2020 at 17:02

0

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.