Ask Leo! by Leo A. Notenboom

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

Search First! Then browse: Categories | Full Archive | By Date | Newsletter

Home » Windows » Windows Programs

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.

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.

Related:

Helpful? Get new articles weekly by email in my FREE newsletter!

Your Name:
Your Email:


Why Subscribe?

Article C3026 - May 16, 2007

Recent Comments
18 Comments

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

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

Posted by: Tim at January 17, 2008 7:43 PM

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.

Posted by: The Frugal Geek at August 9, 2008 11:03 AM

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.

Posted by: Les Elton at August 27, 2008 5:54 PM

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

Posted by: anth11 at October 14, 2008 3:01 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.

Posted by: MasterTech at October 15, 2008 3:58 PM

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!

Posted by: jubilee at March 31, 2009 11:47 AM

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

Posted by: geo-girl at August 16, 2009 3:29 PM

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

Posted by: Dmitri Poliski at November 21, 2009 3:50 AM

Post a comment on "How do I schedule Disk Defragmenter to run with the Windows Task Scheduler?":






(Email Address will not be published.)

Remember Me?

By popular demand...
my tip jar
Cuppa Joe
Buy Leo a Latte!

(you may use HTML tags for style)

RSS feed Subscribe to the RSS Feed specifically for comments on this article.

Before commenting, please...

  • Read the article at the top of this page. If your comment shows you didn't, it'll be deleted and ignored.

  • Comment only on this article. Use the Google search box at the top of the page if you have a question about something else.

  • Don't include personal information in the comment. No email addresses. No phone numbers. No physical addresses.

  • Don't spam. Excessive links to unrelated sites within a comment or across multiple comments will cause all such comments to be removed.

  • Don't ask me to recover lost passwords or hacked accounts. I can't, and those comments will be deleted.

  • I can't respond to every comment. And I can't vouch for the accuracy of others who do.

Please wait. Your comment is being processed ...


Question? Ask Leo!