Returning Codes from PowerShell
From TekiWiki
PowerShell - the scripting language from Microsoft - often needs to be called from a command line. Often the calling program then needs to receive and inspect the exit code from the script to see if the PowerShell script completed OK.
Example
For instance, say we have the following code in a script called bad.ps1:
## bad.ps1 ## Something went wrong - let everyone know exit 7
...and then we call this suffixed by "exit $LASTEXITCODE":
rem bad.bat rem Run our script cmd.exe /c "powershell.exe -ExecutionPolicy RemoteSigned C:\wherever\bad.ps1;exit $LASTEXITCODE" rem Check if we can see the error echo %ERRORLEVEL%
And so we see the correct error level in the calling program (in our case the batch script):
C:\>rem bad.bat C:\>rem Run our script C:\>cmd.exe /c "powershell.exe -ExecutionPolicy RemoteSigned C:\wherever\bad.ps1;exit $LASTEXITCODE" C:\>rem Check if we can see the error C:\>echo 7 7