A Step-by-Step Guide to Using ERRORLEVEL and EXIT Commands for Reliable Script Automation
Batch files are powerful tools for automating tasks in Windows environments, and understanding how to properly handle exit codes is crucial for creating robust and reliable scripts. This comprehensive guide will walk you through everything you need to know about batch file exit codes, from basic concepts to advanced implementation techniques.
Batch file exit codes, also known as errorlevels, are numeric values that indicate the completion status of a batch script or command. These codes serve as a communication mechanism between your batch file and the calling process, whether it's another batch file, a command prompt, or a scheduling system.
Exit codes follow a standardized convention:
0: Successful execution (no errors)
1-255: Various error conditions or custom status codes
Negative values: System-level errors (less common in batch files)
The term "errorlevel" originates from DOS and refers to the environment variable that stores the exit code of the last executed command. Understanding this concept is fundamental to creating professional-grade batch scripts that can be integrated into larger automation workflows.
When a batch file executes, Windows tracks the exit status of each command. The ERRORLEVEL environment variable automatically updates to reflect the exit code of the most recently executed command. This mechanism allows you to create conditional logic based on command success or failure.
Different applications and commands return specific exit codes to indicate various conditions:
| Exit Code | Meaning |
|---|---|
| 0 | Success – The command completed without errors |
| 1 | General error – A catch-all for most error conditions |
| 2 | Misuse of command – Incorrect syntax or parameters |
| 126 | Command cannot execute – Permission or path issues |
| 127 | Command not found – The specified command doesn't exist |
| 128+ | Fatal error signals – System-level interruptions |
The most straightforward way to return an exit code from a batch file is using the EXIT command with the /B parameter:
@echo off
echo Processing data...
if exist "required_file.txt" (
echo File found, processing...
echo Process completed successfully
exit /b 0
) else (
echo Error: Required file not found
exit /b 1
)
The /B parameter ensures the batch file exits without closing the entire command prompt window, returning control to the calling process with the specified exit code.
You can manipulate the ERRORLEVEL variable directly using various techniques:
@echo off
echo Starting validation process...
REM Method using CMD /C EXIT
cmd /c exit 5
echo Current ERRORLEVEL: %ERRORLEVEL%
REM Method using color command with invalid parameter
color 00 2>nul
if %ERRORLEVEL% neq 0 (
echo Color command failed as expected
)
Many Windows commands naturally return meaningful exit codes that you can propagate:
@echo off
ping google.com -n 1 >nul
if %ERRORLEVEL% equ 0 (
echo Network connection successful
exit /b 0
) else (
echo Network connection failed
exit /b 2
)
Mastering exit codes in batch files is essential for creating professional, maintainable automation scripts. By implementing proper exit code handling, you enable better error detection, facilitate debugging, and improve integration with other systems and scripts.
Remember these key points:
exit /b to return exit codes without closing the command promptERRORLEVEL values immediately after critical commandsWith these techniques and best practices, you'll be able to create robust batch files that communicate effectively with their calling processes and provide clear feedback about their execution status. For deeper scripting capabilities, consider exploring PowerShell as a modern alternative that offers more advanced error handling and automation features.
Run, Monitor, and Manage Scripts Across All Your Endpoints, From One Central Console
Managing batch files and exit codes manually across dozens or hundreds of Windows machines is time-consuming and error-prone. Zecurit's Remote Script Execution lets IT teams deploy, run, and track batch scripts enterprise-wide, with real-time exit code monitoring, instant error alerts, and full execution logs, so you always know what ran, where, and whether it succeeded.
Execute batch scripts remotely across all endpoints without touching each machine
Monitor exit codes in real time and get instant alerts on script failures
Schedule automated script runs to keep your environment consistently configured
View full execution logs for complete audit trails and faster troubleshooting
Integrate with patch management and software deployment for end-to-end automation
An exit code (also called an errorlevel) is a numeric value returned by a batch script or command to indicate whether it completed successfully or encountered an error. A value of 0 means success, while values 1–255 represent various error or custom status conditions.
EXIT closes the entire command prompt window along with the script, while EXIT /B exits only the current batch script and returns control to the calling process, without closing the command prompt. Always use EXIT /B when you want to return an exit code safely.
You can check it using the built-in %ERRORLEVEL% environment variable, which automatically updates after every command is executed. For example: if %ERRORLEVEL% equ 0 (echo Success) else (echo Failed)
Because %ERRORLEVEL% gets overwritten by every subsequent command, including echo. If you don't capture or check it right away, you risk reading the wrong value and making incorrect decisions in your script logic.
Yes. You can use any numeric value between 1 and 255 as a custom exit code to represent specific outcomes in your script. It's best practice to document what each code means so other scripts or systems calling your batch file can interpret the results correctly.
Stop running batch files one machine at a time. Zecurit's Remote Script Execution empowers IT teams to deploy and monitor scripts across every endpoint, with real-time exit code tracking, failure alerts, and complete audit logs.