Windows XP's built-in hard disk defragmenting software, Disk Defragmenter, doesn't have a scheduling feature. Some commercial disk defragmentation applications have scheduling capabilities, but they're costly. I did some research and found that you can schedule XP's Disk Defragmenter with the Task Scheduler.
I work in an enterprise environment with many workstations, so I decided to write a logon script to automate the scheduling process. The ScheduleDefrag.cmd script uses the Schtasks utility (the command-line interface to Task Scheduler) to create a scheduled task named Weekly Defrag. The Weekly Defrag task uses defrag.exe (the command-line interface to the Disk Defragmenter) to defrag the hard drive.
ScheduleDefrag.cmd starts by checking to see whether the Weekly Defrag task has already been scheduled with the code
Schtasks /Query /FO LIST | Find /c "Weekly Defrag"
This code uses the Schtasks command with the /Query parameter and its /FO LIST switch to retrieve all the tasks already scheduled on the system. The results are piped (|) to the Find command, which searches for the string Weekly Defrag.
If the Weekly Defrag task doesn't exist, the script uses the following Schtask command to create it:
Schtasks /Create /RU "SYSTEM" /SC WEEKLY /D FRI /TN "Weekly Defrag" /TR "%systemroot%\system32\defrag.exe %homedrive%" /ST 12:00:00 /SD 10/01/2009
The /Create parameter tells Schtasks to create a scheduled task. I'll go through the switches I used with that parameter so you can modify the script to meet your defragmentation scheduling needs:
- The /RU switch identifies the account with which to run the task. In this case, the task will run under the System account. The other system account you can use is "NT AUTHORITY\SYSTEM".
- The /SC switch indicates how often to run the task. In this case, the frequency is weekly, but there are other options, such as daily and monthly.
- The /D switch denotes the day of the week to run the task.
- The /TN switch provides the name of the task being scheduled.
- The /TR switch specifies the task to run. In this case, Schtasks will run defrag.exe on %homedrive%. The %homedrive% environmental variable specifies a computer's local drive, which is typically the C drive.
- I try to use environmental variables as much as possible when scripting. If you hard-code the information, you must modify the script each time it changes.
- The /ST switch indicates the task's start time. You must use a 24-hour clock and follow the format hh:mm, where hh is the hour and mm is the minute.
- The /SD switch provides the task's start date. You must follow the format mm/dd/yyyy, where mm is the month, dd is the day, and yyyy is the year.
There are many other switches you can use with the Schtasks /Create command. For a list of them, you can type
Schtasks /Create /?
on the command line or go to MSDN's Schtasks.exe web page.
You can download ScheduleDefrag.cmd by clicking the Download the Code Here button at the top of the page. To customize the script to meet your scheduling needs, right-click the file, select Edit, make the necessary changes to the Schtasks /Create command, then save the file.
You can run ScheduleDefrag.cmd as a logon script if you want to schedule the Disk Defragmenter on many machines, or you can run it locally if you want to schedule the Disk Defragmenter on only a few machines. To run it as a logon script, create or open an existing Group Policy Object (GPO), navigate to User Settings\Windows Settings\Scripts\Logon, and add the code in ScheduleDefrag.cmd to the Logon scripts dialog box. To run ScheduleDefrag.cmd locally, place it on the machine and double-click it or run it from the command line.