Helping people with computers... one answer at a time.

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.

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:

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:

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.

Article C3026 - May 16, 2007 « »

Share this article with your friends:

Share this article on Facebook Tweet this article Email a link to this article
Leo Leo A. Notenboom has been playing with computers since he was required to take a programming class in 1976. An 18 year career as a programmer at Microsoft soon followed. After "retiring" in 2001, Leo started Ask Leo! in 2003 as a place for answers to common computer and technical questions. More about Leo.

Not what you needed?

25 Comments
dave b
May 16, 2007 9:52 PM

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.

al
May 18, 2007 7:16 PM

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?


Alan
=====

Dr. Arthur Billy
May 19, 2007 10:35 AM

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 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

David Heym
May 22, 2007 8:07 AM

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?

David Heym
May 22, 2007 8:52 AM

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.

Leo A. Notenboom
May 22, 2007 3:58 PM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

Leo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)

iD8DBQFGU3V8CMEe9B/8oqERAs+3AJ0SjhkTgHefkimM9Xzs1NApmec9/ACbBb77
14Ddmo+QnAo+kXNIhZIQUXg=
=L3FS
-----END PGP SIGNATURE-----

Troy
June 5, 2007 7:02 PM

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.

Katie Heath
June 27, 2007 12:38 AM

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?

Douglas Gray
October 19, 2007 6:10 AM

"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

darlene
November 3, 2007 5:33 PM

I tried to do this, and it says I do not have permission to set the scheduled task, what do I do?

Tim
January 17, 2008 7:43 PM

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?

The Frugal Geek
August 9, 2008 11:03 AM

Hi 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.

Les Elton
August 27, 2008 5:54 PM

There 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.

anth11
October 14, 2008 3:01 PM

To Frugalgeek.
Don't worry, Vista automatically defrags every week.
anth11

MasterTech
October 15, 2008 3:58 PM

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.

jubilee
March 31, 2009 11:47 AM

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!

geo-girl
August 16, 2009 3:29 PM

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.
Leo
17-Aug-2009

Dmitri Poliski
November 21, 2009 3:50 AM

try defraggler (http://www.piriform.com/defraggler) - freeware that WORKS !!
Dmitri

Adviper
February 1, 2010 12:17 AM

command C:\WINDOWS\system32\defrag.exe c: is only for C drive. How about command on how to defrag drive c:, d: e:? Please advice. Thanks.

richard borgman
March 22, 2011 8:30 AM

don't know why i give my email you never respond
i checked task scheduler and it does not list defrag in win 7

This newer article has a picture of Defrag in Task Scheduler: How often should I defragment my hard disk? - You might also want to read this article: Why didn't you answer my question?
Leo
24-Mar-2011

Bill R TechSpec
March 31, 2011 10:12 PM

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}


LINDA SNIDER
February 4, 2012 3:02 AM

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

connie
February 4, 2012 9:32 AM

@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

Sylvia
October 12, 2012 4:45 PM

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
Sylvia

connie
October 12, 2012 8:09 PM

@Sylvia,
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?

Comments on this entry are closed.

If you have a question, start by using the search box up at the top of the page - there's a very good chance that your question has already been answered on Ask Leo!.

If you don't find your answer, head out to http://askleo.com/ask to ask your question.