Random XP tip: Scheduled disk defragmentation


It came to my attention that a lot of folks don't know about one of the very nice additions that came long with the rest of Windows XP: A command-line interface to the disk defragmenter. This means it's now easy to setup a scheduled task to defrag your hard disk(s), whereas with Windows 2000 it was rather difficult.


If you're already familiar with scheduled tasks, all you need to know is that the tool is located at %WinDir%\system32\defrag.exe, and takes the drive letter as an argument (e.g. "c:\winnt\defrag.exe c:".


If you've never created/managed a scheduled task before, don't worry - it's not very hard:



  1. Bring up Control Panel
  2. Double-click on "Scheduled Tasks"
  3. Double-click on "Add Scheduled Task"
  4. Click "Next", then click "Browse"
  5. Type (or paste in) %windir%\system32\defrag.exe (then click Open)
  6. Change the name of the task from "defrag" to "Defrag C" (note that you can't put a colon in the name of the task)
  7. Select "Weekly" for the task interval (if you're really obsessed with performance, you can set it to daily), press "Next"
  8. Change the start time - I usually use 04:00am. Select a day of the week. The only caveat here is to be sure to pick a time the machine is actually on (so if you turn your computer off every night, 4am is probably not the best choice); press Next
  9. Enter an administrator user and password, press Next
  10. Check the box for advanced properties, then click Finish.
  11. When the advanced properties come up, add the drive letter to the end of the run box, you should see something like this afterwards (note the colon): C:\WINNT\system32\defrag.exe c:
  12. You shouldn't need to change any of the other settings, but feel free to look them over

I assure you the fact that this was a 12-step process is entirely coincidental. If you have multiple hard drives, repeat steps 3-12 for each - be sure each task has a unique name.


When you're done, you should see something like this:



That's all there is to it! You may want to bring up scheduled tasks every so often and make sure they're working as expected (last result is 0x0). If you see 0x7, that means the disk has less than 15% free space and therefore defrag.exe won't run by default - you can add -f to the command if you want it to defrag anyway, but I generally advise freeing up some space instead. If you see 0x1, the password is probably wrong or out of date.

Comments (5)

  1. BlakeHandler says:

    You can also add the /sageset & /sagerun command to set the defaults for the program.

    http://spaces.msn.com/bhandler/Blog/cns!1pt1v0Q4vD8jSvNS4lqdAuug!373.entry

  2. CRathjen says:

    Interesting (and apparently undocumented). I’ll have to give that a try!

  3. Individualist says:

    /sageset and /sagerun are for automating Disk Cleanup "cleanmgr.exe", it doesn’t seem to work with the command line defrag (defrag.exe) or GUI defrag (dfrg.msc)

    But it’s still useful and related to automating maintance tasks.

    cleanmgr.exe /sageset:n will open Disk Cleanup as usual, but when you pick your options and click ok, it doesn’t do the clean, it just stores the configuration. Later you can run it with those options with cleanmgr.exe /sagerun:n.

    The "n" in /sageset:n and /sagerun:n is whatever number you want.  You can store different settings each with a different number, then use those settings with /sagerun:n

    e.g. Run cleanmgr.exe /sageset:5 and set your settings, then use scheduled tasks to schedule cleanmgr.exe /sagerun:5, and it will run them automatically.

  4. Evan says:

    For all kinds of automated tasks I strongly recommend VisualCron.

    // Evan

  5. CRathjen says:

    I looked at the site – I’ll definitely have to take this for a spin. Thanks!

Skip to main content