Windows: Get Exit Code (ErrorLevel) – CMD & PowerShell

Every command or application returns an exit status, also known as a return status or exit code.

A successful command or application returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code.

In Linux you can get the exit status of a last command by executing echo $?.

In this article i will show how to get the return code from the last console command or application in Windows using the command-line prompt (CMD) or the PowerShell.

Exit Code Of Last Console Command

Return True or False depending on whether the last console command or application exited without error or not:

# Windows CMD
C:\> if %ErrorLevel% equ 0 (echo True) else (echo False)

# Windows PowerShell
PS C:\> $?

Get the exit code of the last console command or application:

# Windows CMD
C:\> echo %ErrorLevel%

# Windows PowerShell
PS C:\> $LastExitCode

Exit Code Of Windowed Application

Return True or False depending on whether the last windowed application exited without error or not:

# Windows CMD
C:\> start /wait app.exe
C:\> if %ErrorLevel% equ 0 (echo True) else (echo False)

# Windows PowerShell
PS C:\> app.exe
PS C:\> $?

Get the exit code of the windowed application:

# Windows CMD
C:\> start /wait app.exe
C:\> echo %ErrorLevel%

# Windows PowerShell
PS C:\> app.exe
PS C:\> $LastExitCode