How to get a returned value from powershell and get it in a batch file?

Question:

I’m trying to execute a powershell from a batch file with the commande:
Powershell .\nameoffile.ps1

The PowerShell returns some values 1, 4, 0 and -1 . How can I get these values from the batch? When I use %errorlevel% it only returns 0 (which means that the script is okay). I have also tried using the Exit command in PowerShell (Exit 4) but It does not work.
Can you help me?

EDIT
I have found a solution if someone is interested.

powershell "&{.\test.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%

Answer:

Source:

How to get a returned value from powershell and get it in a batch file? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply