Summary: You want to defragment your hard disk periodically. Task Scheduler runs programs periodically. Put those together to defragment your disk regularly.
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.
•
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.

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

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

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:

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

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.

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:
defrag >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 a 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:

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

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.
Related:
Ask Leo! - How do I defragment my new hard disk?
Ask Leo! - How do I schedule a program to run on my machine periodically?
Article C3026 - May 16, 2007
"Wow, incredible astroturfing from the people at Diskeeper. Could you make more obviously biased comment spam? Not a chance I'd ever try, let alone buy, your product now (and most of your comments are wrong). Diskeeper: either your sales policy is sleazy or an individual there is.
Posted by: Troy at June 5, 2007 07:02 PM"
Dear Troy
Of course you do realise when you state "Not a chance I'd ever try, let alone buy, your product now" that the Windows defrag routine is a cut-down version of Diskeeper. Let us know how you get on with the uninstall.
Cheers
Douglas
Posted by: Douglas Gray at October 19, 2007 6:10 AMI tried to do this, and it says I do not have permission to set the scheduled task, what do I do?
Posted by: darlene at November 3, 2007 5:33 PMDefrag 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?
Posted by: Tim at January 17, 2008 7:43 PMHi Leo, Great instructions! I posted a link on my new blog:
http://thefrugalgeek.blogspot.com/
Let me know if you have instructions for Vista, etc.
Posted by: The Frugal Geek at August 9, 2008 11:03 AMThere were some previous comments to recommend Diskeeper. Diskeeper is an excellent product... on the workstation but has serous misgivings on a Server ! I have found Diskeeper on the server to create problems on our Database servers and email servers. Basically Diskeeper on the server creates timing problems when opening files, that create errors in both our Pervasive SQL database and our Mercury/32 email server.
We had to uninstall Diskeeper to keep those two programs running smoothly.
We have a similar issue with Undelete (similar product from Diskeeper), that creates timing issues, but so far we have been able to work around it by specifying exactly which files we want to monitor for deletion and which we do not. Kind of a pain, but there product has saved us from grief which is the only reason why its still installed.
Windows 2003 32bit server environments
Les Elton, Network Admin., ABC Recycling Ltd.
Posted by: Les Elton at August 27, 2008 5:54 PMTo Frugalgeek.
Posted by: anth11 at October 14, 2008 3:01 PMDon't worry, Vista automatically defrags every week.
anth11
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.
Posted by: MasterTech at October 15, 2008 3:58 PMI 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!
Posted by: jubilee at March 31, 2009 11:47 AMWindows 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?
17-Aug-2009
Posted by: geo-girl at August 16, 2009 3:29 PM
try defraggler (http://www.piriform.com/defraggler) - freeware that WORKS !!
Posted by: Dmitri Poliski at November 21, 2009 3:50 AMDmitri