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.
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:
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:
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.
20 comments on “How do I schedule Disk Defragmenter to run with the Windows Task Scheduler?”
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.
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?
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 http://www.diskeeper.com 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
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?
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.
—–BEGIN PGP SIGNED MESSAGE—–
Access denied implies to me that you need to be logged in as administrator to
do the defrag.
—–BEGIN PGP SIGNATURE—–
Version: GnuPG v1.4.6 (MingW32)
—–END PGP SIGNATURE—–
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?
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?
Hi Leo, Great instructions! I posted a link on my new blog:
Let me know if you have instructions for Vista, etc.
Don’t worry, Vista automatically defrags every week.
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:
When you use defrag from the command line you always have to specify the target drive.
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!
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.
try defraggler (http://www.piriform.com/defraggler) – freeware that WORKS !!
don’t know why i give my email you never respond
i checked task scheduler and it does not list defrag in win 7
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:
I READ MOST OF THE DISK DEFRAGMENTER, I’M STILL NOT TO SURE ABOUT IT. KINDA SCARE I MIGHT LOSE LOSE MY FILES AND ECT……. CAN YOU HELP ME? WHAT SHOULD I DO? THANK YOU LINDA
You should also be doing a regular backup of your system. Then you can feel confident about always having safe data. Leo’s webinar on backups gives some great choices on how to do it:
Webinar Backup Sampler
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
Here’s Leo’s great article on how to set your admin permissions:
Why does my computer say I need to be administrator, when I am?