Data Centers

Defragment your Windows Server 2003 hard drive on a set schedule

You can defragment the hard drives on your Windows Server 2003 boxes on a set schedule by simply creating a batch file to defragment a drive and setting up a scheduled task to execute it.

You can defragment the hard drives on your Windows Server 2003 boxes on a set schedule by simply creating a batch file to defragment a drive and setting up a scheduled task to execute it.

Creating a .bat file

To create this little piece of code, open a new file in your favorite text editor, such as Notepad. You will also need to open the Windows command shell (cmd.exe). The command shell will help you nail the syntax of the operation so there are as few hiccups as possible.

  1. In the command prompt window, enter the following:

    C:> defrag.exe /?

    The help documentation will display for defrag.exe, showing the syntax you need to run the application and any optional switches.
  2. To keep it simple and to restrict the defrag operation to one drive, you can enter the syntax for our batch file into the text editor as follows: Defrag.exe c: -f
  3. Save the text file with a name and the .bat file extension.
  4. You will also select all files in the file types drop down.

Scheduling the execution of fragments To schedule the batch file to run, we can run an AT command from the command prompt. AT commands add Scheduled Tasks without going through the GUI-based application; this can speed up the creation of scheduled tasks. For more information about AT commands, read the TechRepublic article, "Buy some time by automatically scheduling administration chores."

In the command prompt window, press [Enter] a few times to get a C:\> prompt within view. Enter the following to see the syntax and examples of the AT command:

AT 02:30am /every:M,T,W,Th,F c:\defragment.bat

Note: Walking through the entire process will help to familiarize you with all of the available options for commands used in this tip. The help display is for demonstration purposes only; you do not need to perform it.

Press [Enter] after entering the command, and a scheduled defragmentation of the local C: drive will take place weekdays at 2:30 in the morning.

You can verify the task by visiting the Scheduled Tasks folder in the Accessories folder of the Start menu.

Miss a column?

Check out the Windows Server 2003 archive, and catch up on the most recent tips from this newsletter.

Stay on top of the latest Windows Server 2003 tips and tricks with our free Windows Server 2003 newsletter, delivered each Wednesday. Automatically sign up today!

About

Derek Schauland has been tinkering with Windows systems since 1997. He has supported Windows NT 4, worked phone support for an ISP, and is currently the IT Manager for a manufacturing company in Wisconsin.

Editor's Picks