Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

I run maintenance for a lot of offices and use chkdsk as a part of the maintenance. I've recently been using PSEXEC to start a batch file that simply runs chkdsk and then pauses to keep the command prompt window open so that I can check to see if there were any errors found.

I would like to automate the process a little more.

I read somewhere (sorry my browser is not letting me cut and paste the link to the TechNet article) that chkdsk has exit codes that can be used to determine if any errors were found or not.

My problem is that I do not know where to begin in making use of these error codes? Could anyone shed some light or point me in the right direction on what I need to learn so that I can make a batch file or script that can run the chkdsk and then perform another action depending on the exit code?

share|improve this question

The batch file is interpreted by CMD. The exit status of a command is stored in %errorlevel% variable.



goto error_%ERRORLEVEL%
echo Ok
echo Exit code 1.

The if command in cmd can test for the status of the last command:

IF ERRORLEVEL 0 goto okay
echo Error.
goto end
echo Normal exit.

For details run inside cmd:

 help if
share|improve this answer
The possible exit codes for ChkDsk is documented on TechNet. '0: No errors were found. 1: Errors were found and fixed. 2: Performed disk cleanup (such as garbage collection) or did not perform cleanup because /f was not specified. 3: Could not check the disk, errors could not be fixed, or errors were not fixed because /f was not specified.' – abstrask May 9 '13 at 20:04
To add, you can also pipe the output using chkdsk > C:\chkdsklog.log first – TheCleaner May 9 '13 at 20:44

ok here's a link to do this via powershell which I'd think would be more robust then a psexec solution (not that psexec is bad).

share|improve this answer
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – EEAA May 9 '13 at 20:56

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.