Get return code from "second" powershell.exe in cmd RRS feed

  • Question

  • Im trying to get the errorcode from the "second" powershell process in cmd.

    If I run this in cmd: powershell.exe -noprofile -command "&{ start-process -FilePath 'powershell.exe' -ArgumentList '-noprofile get-service' -verb RunAs}"

    echo %errorlevel%  returns the value of the "first" powershell.exe.

    • Edited by DavidARZ Thursday, February 7, 2019 1:26 PM
    Thursday, February 7, 2019 1:26 PM

All replies

  • Here is a similar question whose answers may help you.


    Your "inner" script may need to set a separate environment variable (or write the status to a file)  if you need to pass a status back to the bat file.  As you have found, %errorlevel%  returns the value of the "first" powershell.exe.

    Thursday, February 7, 2019 3:37 PM
  • Why using cmd at all? If you like to run Powershell scripts you should use Powershell. Take a look at

    Get-Help about_Requires

    Live long and prosper!


    Thursday, February 7, 2019 4:47 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,


    Just do it.

    Thursday, February 21, 2019 6:59 AM