How to get the error code (ErrorLevel) from PowerShell into Windows command prompt?

17,757

Try this code in cmd.exe shell :

C:> powershell -noprofile -command " try { 1/0 } catch {exit 22}"

C:> echo %errorlevel%
22

So, add your commands in try block. If it happens that you call non powershell command you could exit $LastExitCode (but from within try block).


EDIT

OK, I know what is going on with your non-working code, it never passed trough catch block as Get-Content produces non-terminating error. You have to take care about that:

C:> Powershell -Command "try { (Get-Content HvsvmAppserver.ref -ea stop) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref -ea stop } catch {$_; exit 123 }"

Get-Content : Cannot find path 'C:\Users\majkinetor\HvsvmAppserver.ref' because it does not exist.
At line:1 char:8
+ try { (Get-Content HvsvmAppserver.ref -ea stop) -replace '-Dfr.base0. ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\majkinetor\HvsvmAppserver.ref:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

C:> echo %errorlevel%
123

Notice the -ea stop that I added to both commands (shorter then ErrorAction). My solution was correct the first time, however, you need to understand that script must be designed correctly too. You might also want to use trap instead catch, but you still have to use EA stop. Perhaps the shortest general solution that doesn't depend too much on a script is:

   $ErrorActionPreferance = 'Stop'
   <your script goes here, no need for -ea stop in it>
   trap { exit 123 }

This will not work if the script in question changes $ErrorActionPreference (which rarely happens) but will work in all other cases.

Share:
17,757
JustGreat
Author by

JustGreat

Updated on June 19, 2022

Comments

  • JustGreat
    JustGreat almost 2 years

    I am trying to launch a PowerShell command through Windows command prompt to replace a text into a file. The problem is that I am always getting ErrorLevel = 0 even when my PowerShell command is not working. My question is how to get an error code into ErrorLevel when I launch PowerShell through cmd? Please note that I would like to make this through the Windows command prompt and not by creating a script a part.

    Here is an example of what I am saying. The error in the print screen is made intentionally by choosing an non existing file to show you that even when there is an error, the ErrorLevel is equal to 0.

    The command I am using is the following (I tried $LastExitCode and $ExitCode):

    powershell -NonInteractive -NoProfile -Command "(Get-Content HvsvmAppserver.ref) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref; exit $LastExitCode"
    

    But I also tried the same with:

    powershell -NonInteractive -NoProfile -Command "(Get-Content HvsvmAppserver.ref) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref; exit $ExitCode"
    

    Based on the comment of majkinetor, I tried the following and hasn't work as well :(, I am keep getting ErrorLevel 0 in Dos even when an error occurs in powershell.

    powershell -noprofile -command " try { (Get-Content HvsvmAppserver.ref) -replace '-Dfr.base0.role=', '-Dfr.base0.role=USR_ROLE'| Set-Content -encoding ASCII HvsJvmAppserver.ref } catch {exit $LastExitCode}"
    

    You can see clearly an error related to the file name that it doesn't exist, but the error level is always 0.

    What I want is to get an error code other than the 0 for each error that happens while executing the PowerShell command.

    Based on some comments I read, I would like to say that : I want to make it by PowerShell and through Dos command and not with a different way.I don't want to make my own error code or personnalized error management ! All I want is that when an error occured in my powershell (code, scripts etc...), the error level of Dos get this error code (I don't want to make my own code). The ones who have an answer are welcome and thanks for them, but the ones that are here just to comment with unhelpfull comments by asking to change what I want, or to delete pics etc...please don't comment except to provide a help and an answer to my question not by proposing something else, I am asking for a help about something specific, kindly don't change the subject to something else.. The idea of this site is to provide help, not to add useless and unhelpfull comments.

    Error 2