Defrag from Windows Vista’s Command Line

As usual, you’ll find more functionality by abandoning the GUI

Mark Minasi

March 26, 2008

3 Min Read
ITPro Today logo in a gray background | ITPro Today

 


Microsoft has included an NTFS disk-defragmentationtool in Windows Server productssince Windows NT 4.0. In fact, it seems as ifevery new Windows version comes with a brand-newdefrag tool, and Windows Vista is no exception. Vista’sGUI-based defragmenter has taken a lot of heat for itstaciturnity: Whereas previous GUI defraggers offereda graphical representation of your disk’s used and freespaces, Vista’s GUI defragger merely asks that you trustin its effectiveness and not worry about the details. However,even if the tool is doing a good job, I prefer to knowwhat my system is doing.

So, I turned to the command-line defragger, Defrag(defrag.exe), to see whether it offered a bit more functionalitythan the GUI tool. As is so often the case, itdid.

Basic Defrag


One of the cool things about Defrag is its report, whichyou can get with the tool’s -a option (for the basic report)or its -a -v option (for the more detailed report). Invokethe command as follows:

 defrag  -a -v

The basic report displays the size of the largest block offree space and the overall percentage of fragmented files,followed by a line of advice about whether you shoulddefragment the drive. The addition of -v adds about 20factoids concerning your system, including the averagesize of your files, the degree of fragmentation in NTFS’sMaster File Table (MFT), the total number of fragmentedfiles, and so on.

Don’t forget to run Defrag from an elevated Vistacommand prompt—by choosing the Run as Administratoroption when you open the command prompt.Defragging involves reading and writing files, andbecause only administrators have the power to read allfiles, Defrag probably requires this privilege elevation.Defragging a drive is easy: Simply follow the commandwith the letter of the drive to defragment. Forexample,

 defrag C:

That command performs a quick, simple defragmentationthat merely attempts to bring together fragmentsthat are smaller than 64MB. The reason behind thatlimitation is that you won’t see much performanceimpact in joining monstrous hunks of data, comparedwith joining smaller fragments. For example, sequentiallyreading a file broken into ten 500MB fragmentswould certainly be slower than reading one 5000MBchunk, but not by much. But consider reading that samefile as 500 10MB fragments. The drive’s read/write headwould be dancing all over the platter!

However, what if you want to go ahead and defragmentfragments smaller than 64MB? Then just add the-w option, as in

 defrag C: -w

Of course, that job will take longer—much longer,in some cases. When I tried defragging my 160GB Cdrive with the -w option after running Vista for ninemonths, the procedure took four hours. But, again, if“sleek is what you seek” defragging-wise, then –w isthe way.

Heavy Duty


I’ve sometimes been guilty of ignoring the need fordefragmentation until performance is sluggish, and it’sno coincidence that the sluggishness is most obviouswhen I’ve just about filled the drive. Unfortunately, theGUI defragger refuses to attempt a defrag if the drive hasless than 15 percent of its space free. The command-linedefragger, however, offers the -f option, which forcesa defrag even if the drive doesn’t have that much freespace. Now, I’m happy this option exists, but I woulduse this option only in dire circumstances. I’d hate totry to defrag a drive full of important information, onlyto find that the defrag process isn’t so reliable without alot of free space.

Finally, Defrag offers the -c option, which defragmentsall volumes on the target computer. Armed withthis information, I’ve turned off the out-of-the-boxscheduled defragmenter, and built my own heavy-dutydefragger by scheduling the command

 defrag -c -w

Give Vista’s Defrag tool a try. After all, a little morespeed never hurt anyone.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like