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
Was it useful? Share this post with the world!
How to Get the Application Exit Code from a Windows Command Line 💻🔚
So you’re running a program on your Windows command line, and you want to know its exit code, huh? 🤔 No worries, we’ve got you covered! In this guide, we’ll show you how to retrieve the application exit code from your Windows command line and understand what it means. 💡
Understanding the Exit Code 🆘
Before diving into the solutions, let’s first understand what an exit code is. When a program finishes executing, it usually returns a code, called an «exit code,» which indicates its current state. These exit codes help you determine if the program executed successfully or encountered any errors or issues along the way. 🚦
Exit codes are numeric values that can range from 0 to 255. While 0 usually represents a successful execution, non-zero values generally denote different types of errors or warnings. Each program can define its own set of exit codes, so make sure to refer to the program’s documentation for specific meanings. 📑
Solution: Using the ERRORLEVEL
Variable 🛠️💯
When using cmd.exe
on Windows, you can access the exit code of the previously executed program through the ERRORLEVEL
environment variable. Simply follow these steps:
-
Run the program you want to retrieve the exit code from. 🏃
-
Afterward, open your Windows command line and type the following command:
echo %ERRORLEVEL%
-
Press Enter, and voila! 💥 The command prompt will display the exit code of the last program you executed.
Example Scenario 🌟📋
To give you a better idea, let’s go through an example. Say we have a program called «my_program.exe» that validates user input. This program returns an exit code of 1 if the input is invalid and 0 if it’s valid.
-
Run the program by executing the following command:
my_program.exe
-
Once it finishes executing, open your command line and type:
echo %ERRORLEVEL%
-
If the command prompt displays
1
, it means the input was invalid. If you see0
, then hooray! 🎉 The input was valid.
Your Mission: Decode the Exit Code! 🔍
Now that you understand how to retrieve the exit code, it’s time for some real-world problem-solving! 🕵️🔎
Whenever you encounter a program that returns an exit code, use the steps above to decipher what it means. 🤓 You can refer to the program’s documentation or conduct online research to understand the significance of the exit code you receive. By doing so, you’ll quickly identify any errors or warnings and take appropriate action. 💪
Share Your Success! 📣🎉
Now that you’ve mastered the art of retrieving the application exit code from a Windows command line, it’s time to celebrate your newfound knowledge! 🎉
Share this post with your friends, colleagues, or anyone baffled by those mysterious exit codes. Let’s demystify the command line and empower more people to become coding commandos! 💪🔥
Leave a comment and let us know about your experiences with exit codes. Have you ever encountered an unusual exit code? How did you troubleshoot it? We’d love to hear your stories and insights! 📝💬
Remember, exit codes are your secret weapons for taming the command line jungle. Embrace them, understand them, and conquer them! Happy coding! 👨💻💥
Disclaimer: The exit codes mentioned in this post are for illustration purposes only. Make sure to refer to the specific program’s documentation to interpret the correct meanings of its exit codes.
I hope this guide helped you grasp the process of retrieving the application exit code from a Windows command line! Feel free to reach out if you have any more questions. Remember, the command line is your playground — conquer it! 💪🌟
Understanding how to retrieve the exit code of the last command executed is crucial for scripting, automation, and error handling in Windows environments. The exit code provides valuable information about the outcome of the command: a return value of 0 usually signifies a successful execution, while non-zero values typically indicate an error or a specific condition. This numeric code serves as a communication mechanism between scripts or programs and the operating system. This tutorial explains how to get exit code of last command on Windows.
CMD
On Windows, the ERRORLEVEL
is a special environment variable that holds the exit code of the most recently executed command. When a command is executed, it returns an exit code to the operating system.
For instance, in the Command Prompt (CMD) run the ipconfig
command. To get the exit code of the last command, simply type the following command:
echo %ERRORLEVEL%
It returns 0 that indicates that the previously executed command was successful.
PowerShell
In PowerShell, the $LASTEXITCODE
automatic variable holds the exit code of the most recent native program (Win32 application) that was executed. It’s important to note that it does not capture the exit code of PowerShell cmdlets.
$LASTEXITCODE
In PowerShell, the $?
automatic variable offers the execution status of the last command. It holds a value of True
if the last command succeeded and False
if it encountered a failure. Notably, this variable is capable of reporting on both Win32 applications and PowerShell cmdlets.
Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!
We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!
Learn from Guru Rajesh Kumar and double your salary in just one year.
Get Started Now!
rajeshkumar created the topic: get the application exit code from a Windows command line?
Two ways…
(1) The results are stored in a pseudo environment variable named errorlevel so…
echo Exit Code is %errorlevel%
(2) and a special syntax of the if command:
if errorlevel
see if /? for details.
For Example
@echo off
my_nify_exe.exe
if errorlevel 1 (
echo Failure Reason Given is %errorlevel%
exit /b %errorlevel%
)
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
rajeshkumar replied the topic: get the application exit code from a Windows command line?
:: Exit if a required file is missing
@echo off
If not exist MyimportantFile.txt Exit /b
Echo If we get this far the file was found
:: Set the error level to 5
@echo off
call :setError
echo %errorlevel%
goto :eof
:setError
Exit /B 5
To make this more flexible you can change the subroutine to set any errorlevel like this:
:setError
Exit /B %1
Now you can call the subroutine: call :setError 6 replacing 6 with whatever value you need the errorlevel to be set to.
EXIT is an internal command.
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Certification Courses
DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:
- DevOps Certification — Learn the fundamentals and advanced concepts of DevOps practices and tools.
- DevSecOps Certification — Master the integration of security within the DevOps workflow.
- SRE Certification — Gain expertise in Site Reliability Engineering and ensure reliability at scale.
- MLOps Certification — Dive into Machine Learning Operations and streamline ML workflows.
- AiOps Certification — Discover AI-driven operations management for next-gen IT environments.
Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.
Windows System Error Codes (exit codes) Description
Code | Description |
---|---|
0 | The operation completed successfully. |
1 | Incorrect function. |
2 | The system cannot find the file specified. |
3 | The system cannot find the path specified. |
- How do I get an exit code?
- What is the command to exit CMD?
- How do I get the exit code in powershell?
- What is a good exit code?
- Is exit status of last command?
- What is $? In bash?
- How do I close all windows command prompt?
- How do I go back in CMD?
- What is $? In PowerShell?
- How do I return an exit code in Python?
- What does exit 1 do in bash?
- What is exit code 255 in C?
- Who converts the exit status to termination status of a process?
- How do I get an exit code in bash?
- Can exit code negative?
- What is exit code 11 C++?
How do I get an exit code?
To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command.
What is the command to exit CMD?
Alt+F4 (or type “exit” at the prompt): Close the Command Prompt.
How do I get the exit code in powershell?
Steps to return error codes on Powershell scripts:
Use the command Exit $LASTEXITCODE at the end of the powershell script to return the error codes from the powershell script. $LASTEXITCODE holds the last error code in the powershell script. It is in form of boolean values, with 0 for success and 1 for failure.
What is a good exit code?
The normal exit statuses run from 0 to 255 (see Exit codes bigger than 255 posssible for a discussion of why). Normally, status 0 indicates success; anything else is an implementation-defined error.
Is exit status of last command?
gives the exit status of the last command executed. After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 — 255 on error.
What is $? In bash?
$? is a special variable in bash that always holds the return/exit code of the last executed command. You can view it in a terminal by running echo $? . Return codes are in the range [0; 255]. A return code of 0 usually means everything is ok.
How do I close all windows command prompt?
Close all open programs
Press Ctrl-Alt-Delete and then Alt-T to open Task Manager’s Applications tab. Press the down arrow, and then Shift-down arrow to select all the programs listed in the window. When they’re all selected, press Alt-E, then Alt-F, and finally x to close Task Manager.
How do I go back in CMD?
While in the command prompt menu, you can view any files on your computer in a text-only format by typing in the location (usually a disk) and the file name (including any extensions). Type. cd.. into the prompt. After you press Enter, this command tells the program to navigate back to the previous folder.
What is $? In PowerShell?
$? Contains the execution status of the last command. It contains True if the last command succeeded and False if it failed. For cmdlets and advanced functions that are run at multiple stages in a pipeline, for example in both process and end blocks, calling this.
How do I return an exit code in Python?
Exit Codes in Python Using sys
We use the built-in sys module to implement exit codes in Python. The sys module has a function, exit() , which lets us use the exit codes and terminate the programs based on our needs. The exit() function accepts a single argument which is the exit code itself.
What does exit 1 do in bash?
We write “exit 1” in shell script when we want to ensure if our script exited successfully or not. Every script or command in linux returns exit status which can be queried using the command “echo $?”.
What is exit code 255 in C?
Jobs fail on Windows executions hosts with » exit code 255 » when the submission users do not have read and execute permission to the command processor ( cmd.exe ). Grant submission users read and execute permission on cmd.exe to allow job execution without failure.
Who converts the exit status to termination status of a process?
Exit status: is the argument to one of the three exit functions or the return value from main. Termination status: the exit status is converted into a termination status by the kernel when _exit is finally called. If the child terminated normally, the parent can obtain the exit status of the child.
How do I get an exit code in bash?
You can simply do a echo $? after executing the command/bash which will output the exit code of the program. Every command returns an exit status (sometimes referred to as a return status or exit code).
Can exit code negative?
Errno codes are not meant for use as program exit codes, and only in rather special circumstances should you try to exit with a negative status. A common convention is to write an informative error message to stderr ( perror() serves exactly this purpose) and then exit with status 1 or (better, IMO) EXIT_FAILURE .
What is exit code 11 C++?
«Exit code -11» means a segmentation fault. Currently, it is difficult for the develop to localize the crashing point from the log file only.