Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using a couple of servers at work running Windows Server 2008 and noticed that my batch scripts are no longer failing gracefully.

Put simply, any statement checking ERRORLEVEL seems to pass, no matter what exit code you give it. For example, in a fresh command window:

if ERRORLEVEL 1 echo hello

shouldn't print anything, as the previous exit must have been 0. Well, this works fine on my Windows 7 desktop, but not on these servers. This basically breaks all of my scripts.

Is there any patch update or other change that could have been introduced to the servers to make this happen? I'm not the only one with admin rights to these servers, but need a bit more information before pointing the accusatory finger!

share|improve this question
What comes back if you type "echo %errorlevel%" in a command prompt? If it's not coming back as 0, do any scripts set an environmental variable called "errorlevel"? You may be able to check this by typing "set" at the command prompt. – Dan Feb 27 '12 at 14:35
I tested this on Win2k8 & R2 and they worked fine. Last patches were applied in January. – uSlackr Feb 27 '12 at 14:59
%errorlevel% works fine - it's only the built-in I am having problems with. However, relying on %errorlevel% brings its own problems, so I'm reluctant to rewrite my scripts. – RCross Feb 27 '12 at 15:23
Ok, I think I've narrowed down the problem - it doesn't happen with a clean command window on the server until I run a particular command (Gnu wget). After running this command, any checks on errorlevel are broken until I subsequently run a regular batch command (like DIR). Is there any reason a 3rd party command could break this built-in shell variable in this way? – RCross Feb 27 '12 at 15:28
Is there any chance wget is throwing an errorlevel 1 or 2 or x and your script is not catching it? Errorlevels are 'set' by the program and will survive the exit of a batch file (so your 'hello' example works). Errorlevel 1 does not necessarily mean 'fail' to all programs. – RobW Feb 29 '12 at 1:55

Move away from IF ERRORLEVEL. The following is far more reliable (and predictable):

REM *** do something
%SystemRoot%\System32\robocopy.exe blah blah

REM *** now use the RC variable, which won't be interferred with
if %RC% == 0 goto XYZ
if %RC% == 1 goto ABC

I appreciate you're trying to avoid re-writes, but I definitely recommend the above (or PowerShell, of course!).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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