Get exit status of last executed command in Windows?
For many IT administrators, the ability to quickly and easily access the exit status of the last executed command is a must-have. Knowing the status of a command can be used to inform decision-making, troubleshoot issues, or simply provide an understanding of the command’s output.
In Windows, the exit status of a command is referred to as its return code. This return code is an integer value that can range from 0 to 65535. A return code of 0 indicates the command completed successfully, while any other value indicates that an error occurred.
Getting the return code of the last executed command on Windows is a relatively straightforward process. The first step is to open a command prompt window. This can be done by typing “cmd” in the search box of the Start menu. Once the command prompt window is open, the “echo %errorlevel%” command can be used to display the return code.
This command will output a single number, which indicates the return code of the last executed command. If no command has been executed, the output will be “0”. If a command has been executed, the output will be the corresponding return code.
It is also possible to capture the return code of a command in a batch script. This is done by using the “%errorlevel%” variable. This variable can be used to store the return code of a command in a variable for later use.
For example, the following batch script will store the return code of the “ping” command in the “my_return_code” variable:
ping example.com
set my_return_code=%errorlevel%
echo Return code is %my_return_code%
This script will output the value stored in “my_return_code”, which should be the return code of the “ping” command.
Knowing the return code of a command is an invaluable tool for IT administrators. It can be used to quickly identify the cause of an error, or simply to confirm that a command ran successfully. Getting the return code of the last executed command on Windows is a relatively simple process, and can be done using either the “echo %errorlevel%” command or a batch script.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.