I have a .BAT program where I'm calling a Powershell script to run.
How I can get the exit code in the .BAT program when the powershell finishes its work?
EDIT:
This has worked for me.
call powershell.exe .\MyPowerShellScript.ps1
echo %ERRORLEVEL%
The %ERRORLEVEL%
variable will give you the last return code:
@echo off
call powershell.exe -Command "exit 123"
echo Exited with return code %ERRORLEVEL%
Will result in:
C:\> path\to\script.bat
Exited with return code 123
With a script, either use the -File
parameter:
@echo off
call powershell.exe -File "path\to\file.ps1"
echo Exited with return code %ERRORLEVEL%
or use the &
call operator to invoke the script:
@echo off
call powershell.exe -Command "& 'C:\path\to\file.ps1'"
echo Exited with return code %ERRORLEVEL%