Technology in terms you understand. Sign up for the Confident Computing newsletter for weekly solutions to make your life easier. Click here and get The Ask Leo! Guide to Staying Safe on the Internet — FREE Edition as my thank you for subscribing!

How do I schedule Disk Defragmenter to run with the Windows Task Scheduler?


I’d like to automatically defragment my hard disk periodically so I just don’t have to worry about it any more. I’ve heard of the Windows Task Scheduler, can I use that?

Yes you can. In fact, I do.

OK, I run a lot of things in the middle of the night, but periodic defragging is one of them.

Let’s walk through the steps.

Become a Patron of Ask Leo! and go ad-free!

We’ll set up the command line version of the defragmenting tool defrag.exe to run once a week. I’ll arbitrarily pick 3 AM on Sunday mornings, but you can naturally set your own schedule.

Starting in Control Panel, double click on  Scheduled Tasks. Then double click on Add a Scheduled Task.

First screen of the Scheduled Task Wizard

Click Next. You’ll be presented with this list of programs to choose from:

Scheduled Task Wizard - Choose a Program

Unfortunately the tool we want, defrag.exe, isn’t on that list. Click on Browse…:

Select a Program to Schedule Browse Dialog

Browse and you’ll find defrag.exe at C:\Windows\System32\defrag.exe. Click Open. Next, you’ll need to give the task a name and begin choosing the schedule:

Scheduled Task Name and Schedule Dialog

You can choose a different name for the task if you like. Click on Weekly and then click Next:

Select the time and day dialog

As I indicated, I’ve selected 3AM every Sunday. Click Next. Each scheduled task is run as if it were a separate login to your machine, so at this point you’ll need to enter a login account and password that the system should use to run the task.

Scheduled Task Finished

And you’re done! Click Finish, and the task will now run automatically every Sunday at 3AM.

Now, there’s a problem with what we’ve done so far: there’s no log of the results. Defrag might run, but any messages it might output are lost. Unfortunately, there’s no built-in solution, but that doesn’t stop us.

The approach I take is fairly simple. First, use notepad to create a command file containing this single line:

>c:\defrag.log 2>&1

Save that as c:\windows\loggeddefrag.cmd.

That command file runs defrag.exe and saves all normal and error output to the file “c:\defrag.log”. Try it. You can run “loggeddefrag” in a Windows Command Prompt, and when it completes you can have a look at the log it  creates.

Now we just need to update our scheduled task.

Once again in Control Panel, double click on Scheduled Tasks. This time right-click on the task we created: “defrag“, and click on Properties:

Scheduled Task Properties

Now simply change the C:\WINDOWS\SYSTEM32\defrag.exe to the command file we just created: c:\windows\loggeddefrag.cmd:

Scheduled Task Properties Updated

Press OK, and you’ll have to re-enter the password for the account that will be used to run the scheduled task.

Now you’re really done. Check that log file each Sunday morning and you’ll see that the defragmenter has run as well as any errors it might have encountered along the way.

Do this

Subscribe to Confident Computing! Less frustration and more confidence, solutions, answers, and tips in your inbox every week.

I'll see you there!

20 comments on “How do I schedule Disk Defragmenter to run with the Windows Task Scheduler?”

  1. One of the cool aspects of Windows Vista: this is a default behavior. Once a week the disk defragger will kick off – 4:00 a.m. on Sunday morning.

    The only drawback: the cute progress window (showing the fragmentation as the tool ran) has been eliminated in Vista… nothing to watch! If you like metrics, you can run defrag at the command prompt with the -a -v options.

    The Vista version uses fewer system resources and will operate with a drive that is nearly full. Previous versions required 15 to 20 percent empty space on the disk.

  2. I have wanting to do this for awhile. The problem is I am running windows 2000 and when I click on “program files” there is no “system 32”.

    Do these instruction work with windows 2000?


  3. What a convoluted way to schedule the Windows defragmenter which in my opinon as an archaic piece of software.

    I recommend that you ourchase the latest Diskeeper 2007 from You can try it for 30 days before buying.

    Once you install Disjkeper 2007 it runs automatically in the background allof the time without interuption.You do not have to schedule it or stop working as you have to do with the Windows XP defragmenter.I am practically fragment free without doing anyhting. There are other nice things that you can also do with Diskeeper 2007. List Prices range from $29.95 to $129 for the different versions. I use the Diskeeper 2007 Premir Pro. It is fabulous. While you are at it also look at Diskeeper Undelete

    Arthur F. Billy

  4. I followed directions to add the frag log to the running of the defrag routine. It was put into the schedule to run in the early am on Monday (yesterday). When the log was not created, I started investigating. I tried to run defrag from the run box in my XP Service Pack 2 system. The black box flashed momentarily and then vanished. This happened two or three times. I then ran the defrag routine that is a part of system tools and that said I didn’t need to run it, but I did anyway; it took maybe an hour, and then finished fine. Every since service pack 2 certain things I have tried have acted like that: the black run box starting and then quitting without any messages or indications. I suspect there is a fundamental problem with the OS caused by service pack 2, but have no way of knowing what it is and of course of being able to correct it. Is this something that you could address?

  5. Follow up to previous query about defrag. Re: your article on “When I use start-run and type in a command, why does a window just flash and disappear?” Which is what was happening to me. So I followed your directions, using cmd and the window did not disappear. Then when I put in the defrag command exactly as you have it. I get the message: “access is denied”. When I followed the steps above it never asked for any password. I’m the only one using my machine.

    Hash: SHA1

    Access denied implies to me that you need to be logged in as administrator to
    do the defrag.

    Version: GnuPG v1.4.6 (MingW32)


  7. Forgive me if I’m stating the obvious and/or not thinking properly…
    I think the reason it flashes up and disappears again is ’cause it isn’t running. It requires for you to specify the drive after typing in defrag. This is not specified. I changed the loggeddefrag file to “defrag c: >c:\defrag.log 2>&1” but it still didn’t seem to work. Can anyone advise me?

  8. Defrag runs, but stops when it determines defrag is not needed, and waits for user input. Disk cleanup does the same. How do I get the programs to continue without user input?

  9. Ok, to get loggeddefrag.cmd to work right, you need to modify it like this: (without the “”)

    “defrag %* >c:\defrag.log 2>&1”

    The %* will allow the cmd file to pick up command line options when executed.

    To get the scheduled task to run right you need to edit it like this:

    “%path%\loggeddefrag.cmd c:”

    When you use defrag from the command line you always have to specify the target drive.

  10. I want to set up my xp pro machine to do automatic periodic defrag, but my machine is not on all the time. I do not see a check box for “run task if missed” will this happen automatically? Thanks!

  11. Windows XP says I should run de-frag (I did not have it scheduled, only now see your recommendation). But then says I only have 7% space available and should have 15%, do I want to continue?
    What will most likely happen if I try to run defrag in this case?
    It may take a little longer than normal, and not all files will neccessarily defrag.

  12. Very good and detailed directions — my compliments!

    The only downside to using the built-in is that you need to leave your computer on all night (now-a-days not the best thing to do as it wastes energy), so you should select a time when you won’t be using your computer for a while (manual defrags can take several hours depending on the size of the disk and the computer can’t really be used for much while defragging).

    I would recommend a commercial defrag solution that operates in the background without using vital system resources so you don’t have to interrupt your computer use.

    Really, the built-in defrag program is more meant for casual use, sort of like Word Pad or Paint — fine for occasional use, but if you need to write a thesis or design some graphics for pro use, you would use Word for the thesis and maybe In-Design for the graphics, right?

    Likewise if you are a power user, consider a good commercial defrag program.

    Here is a review of the top 11 defrag programs so you can select the one that’s best for your needs:

    {link removed}


  14. I am running Windows XP pro V5.1.2600 SP3.0 32bit
    I followed your instructions to the letter & was ok except when I had finished, it said I didn’t have permission do do this task, I am the owner of the machine, (desktop) How do I get it to recognise me? I’ve not had this problem before.
    Thanks in advance….I also want to congratulate you on a great product, easy to read instructions for us oldies, I just copy & paste to Word, so I can then print it out, no probs….Live long & Prosper, Leo…x


Leave a reply:

Before commenting please:

  • Read the article.
  • Comment on the article.
  • No personal information.
  • No spam.

Comments violating those rules will be removed. Comments that don't add value will be removed, including off-topic or content-free comments, or comments that look even a little bit like spam. All comments containing links and certain keywords will be moderated before publication.

I want comments to be valuable for everyone, including those who come later and take the time to read.