Windows – Defragmenting the Hard Drive

So some of us have the issue where we transfer tons of files between different computers.  As time passes this amount of file transfer added with software installations and removals can quickly make a fast system appear somewhat slower.  Many people might say that a fragmented hard drive will not cause much slowness, but in computer speed even milliseconds are a lot.

Here are a few of the different options you can use with Windows:

  • Manually running the disk defrag program.
  • Creating a command line batch file to run the defrag.
  • Using 3rd party applications to run the defrag.
  • Creating a VBScript to run the defrag command.

For most computer users, manually running the defrag program once every couple months is usually good enough for them.  But for network administrators, serious computer addicts and gamers having the defragment of their hard drives run automatically is an ideal solution.


My second solution is creating a batch file or scheduling a task to run the defrag via command line.  This again is a nice easy solution for personal computers, but a batch file will bring up the Command Prompt while it runs.

The following code is how to defrag by command line:  defrag <drive letter>

Having this box come up could create calls from concerned users in a corporate environment wondering what is going on.  Some corporate environments might install a 3rd party application that can handle the defragmentation of hard drives without bringing up a GUI.

One of the applications I have tested is Diskeeper 2007.  This application provides multiple different options, but the most useful is the automatic defragmenting it can do.  This automatic defragment will run at any time of the day, but users can configure the automatic defrag to not run at times they do not want.


The last option which I like the best and could be used in any type of environment uses a VBScript to find the installed hard drives and defragments each one.  Pair this VBScript with the Task Scheduler and a network administrator will have a valuable defragment option for their environment.

The following code does just that; it queries all drives connected to the computer and then runs a loop that will only defrag the hard drives.  The -f in the run command of the script forces the defrag to run even if there is not enough space on the hard drive or if there is no need for a defrag.  I personally keep it out of my script because I do not think it is needed.

Dim WshShell, objFSO, drive, driveCollection

Set WshShell = CreateObject(“WScript.Shell”)

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Set driveCollection = objFSO.Drives

For Each drive in driveCollection

If drive.DriveType = 2 Then

Return = WshShell.Run(“defrag ” & drive & ” -f”, 1, TRUE)

End If

Next

Set WshShell = Nothing

A network administrator can then save this file to the hard drives of their users’ computers and create a scheduled task that will only run when the computer has been in idle for a certain amount of time.  After the first couple times the defrag runs it then should only have a few files to defrag and will run much faster.  I do not suggest using the run only at idle if this script is going on a server.  For servers, I usually have a reboot command run sometime at night and then after that has finished I have this script automatically run.

I hope this helps explain the many different ways on how to defrag hard drives and even though there are programs out there to run the defrag, administrators can create their own defrag program and run it whenever they need to.

, , , , , , ,

  1. Leave a comment

Leave a comment