Ask Leo!

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

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:

Article 11498 | Posted May 16, 2007

Recent Comments
15 Comments

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

Posted by: Leo A. Notenboom at May 22, 2007 3:58 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.

Posted by: Troy at June 5, 2007 7:02 PM

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?

Posted by: Katie Heath at June 27, 2007 12:38 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

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

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)

New!

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

Before commenting, please...

Please wait. Your comment is being processed ...


Ask Your Question:


ask-leo.com
Web

Stay Informed

Weekly Newsletter

Archives

By Category
By Date

Advertisers

Advertise on Ask Leo!

««   »»

Question? - Ask Leo!
Who is Leo?
Link to Leo!

Terms, Conditions & Privacy