What I have is a batch file that is a wrapper around an installer. This batch file checks the error level returned by the installer and prints accordingly. I noticed that if I execute set ERRORLEVEL=0 in a command prompt right before kicking off the batch file (in the same command window/environment), the installer never messes with the errorlevel and my batch script always returns passed. I would assume %ERRORLEVEL% is a variable defined by windows used specifically to print out errors from programs and scripts and that using the variable in a batch file or something else would be 'at your own risk' because it could be changed at any moment by another process. From what it seems like, when I set errorlevel in the given environment, it then somehow terminates the use of errorlevel as a holder of the exit code. Does anyone know why this is? To me its just weird unexpected behavior. Any information on the subject would be greatly appreciated!


You are redefining the system variable into a regular one. When you do this, the system will not use it until the command session is closed.

The best way would be to use

exit /b 0

in another batch file and call it from your primary script. Where the number 0 is your wanted errorlevel. Either that or use a command that resets the errorlevel for you, such as echo, findstr etc.

For example the following commands would all set ERRORLEVEL to 0 within your batch-file:

VERIFY > nul

cmd /c "exit /b 0"

ver > nul     
  • 2
    Almost correct. You can get the "system" to use the dynamic ERRORLEVEL simply by undefining any user defined value using set "errorlevel=" – dbenham Sep 23 '13 at 19:41
  • Why does Windows allow someone to redefine a system variable into a regular one? Do both definitions still exist, its just echo and other such commands now point to the regular one? So programs could still be updating the system variable, its just not accessible through the command prompt? – user972276 Sep 23 '13 at 19:55
  • 1
    "Either that or use a command that resets the errorlevel for you, such as echo, findstr etc." The echo command does not reset the errorlevel variable. – antred May 11 '15 at 14:33
  • As others have said, if the errorlevel variable has been "manually" set by the user/batch with "set errorlevel=0", the errorlevel variable will no longer reflect the exit code, but you can still access the exit code with if errorlevel do something. – Kevin Fegan Jul 9 '16 at 7:32

You should never assign your own value to dynamic system variables like ERRORLEVEL, PATH, CD, DATE, TIME, etc. Doing so will prevent code from seeing the dynamic value. You can restore the dynamic value by simply undefining the user defined value. For example:

set "errorlevel="

If you want to force the errorlevel to 0, then you can use this totally non-intuitive, but very effective syntax: (call ). The space after call is critical. If you want to set the errorlevel to 1, you can use (call). It is critical that there not be any space after call.

echo %errorlevel%
(call )
echo %errorlevel%

A more intuitive, but less convenient method to set the errorlevel is to use a dedicated subroutine in a batch file:

call :setErr 1
echo %errorlevel%
call :setErr 0
echo %errorlevel%
exit /b

exit /b %1

Or if on the command line, you could use

cmd /c exit 1
echo %errorlevel%
cmd /c exit 0
echo %errorlevel%

I personally use this:

cd .

Works even in unix shell.


Here are some other ways to reset the ErrorLevel state, which even work in MS-DOS (at least for version 6.22):

more < nul > nul

rem // The `> nul` part can be omitted in Windows but is needed in MS-DOS to avoid a line-break to be returned:
sort < nul > nul

The following methods work in MS-DOS only:

command /? > nul

fc nul nul > nul

For the sake of completeness, this sets the ErrorLevel state to 1, valid for both Windows and MS-DOS:

< nul find ""

This is the only thing I have found that works.
call (exit /b 0)

Needed it in a script which uses 3rd party script that detects ERRORLEVEL, but is not resetting error level its self properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.