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?

Recent Comments
25 Comments
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?