you should setup this script as a scheduled job to run after hours.
I?m going to start sharing some useful scripts that I use to automate many processes. Most of the ones I will share, I have gathered from the Internet and edited. Who knows just what I will share, but I can guarantee they can help you out with many system admin tasks!
The first one I will share is a Defragment script I have implemented to run every day on many of the XP workstations I manage. Defragmentation is a big problem if not taken care of every once in a while.
What is defragmentation? When a file is saved to the hard drive, it tries to keep everything in the same location (or cluster) Over the course of time files will be deleted, leaving blank areas between files. If you install another large program, the hard drive tries to fill the holes, thus breaking up the program storage block. This effects your system performance by as much as 200% since programs have to access more areas of the hard drive, just to run.
The script I will show below is based on a threshold of fragmentation. A drive will be defragmented only if it?s fragmentation percentage is determined to be equal to or greater than the FragThreshold value. You can edit to whatever you want.
REQUIREMENTS: The script has to be executed locally on only XP or Server 2003 systems and under account with permissions to defragment local fixed drives.
Defrag All Drives
Source: Windows Admin Script: Defrag Drives Based on Threshold
Originally published on Sun, 22 Apr 2007 00:05:08 GMT by bcarderma