Pluralsight blog Where devs, IT admins & creative pros go for news, tips, videos and more.
Pluralsight + Digital-Tutors - 3,000 tech & creative courses - starting at $29/month Get it now →
August 12, 2010

How To Defrag from the Windows 7 Command Line

By

Troubleshooting computer problems has become a necessary component of being a computer owner. And while problems are inevitable, one of the easiest ways to ward them off is a solid, consistent maintenance routine.

A least loved, but very necessary aspect of that routine should be defragmenting your Windows 7 hard disk.

While previous Windows versions only featured a graphical interface, Windows 7 has included a command-line tool that allows for greater control and flexibility.

Why Do I Need to Defragment?

To help illustrate the importance of defragmenting, we’ll need to understand how the underlying file system works. Hard disks store data in sectors. When the file system on the hard disk is first set up, there are probably a few bits of internal data, but it is mostly one contiguous block of empty space.

When you install your first program, the files are placed sequentially, close to each other. This process goes along fine, as you install new programs or save files. However things begin to go awry when one of a couple things happen: you delete files or perhaps uninstall a program. This new space leaves a gap between the data that was stored before and after your deletion. This little gem is called a fragment.

Additionally, if you modify and append data to an existing file, chances are the new data will have to be written to another area of the disk. So, you guessed it – in time, these fragments can add up. The hard disk arm will have to move all over the surface to find and piece together your data. This is why you sometimes notice operations crawling.

Disk Defragmenter to the Rescue!


In order to rearrange those files into a more logical, contiguous space, you use a disk defragmentation tool. Today we’ll forgo discussion of the gui based tool and delve into the command-line.

You’ll need administrator rights to run the tool. So from the start menu, click All Programs > Accessories. Right-click on command prompt and select run as administrator.

How To Defrag from the Windows 7 Command Line

The defrag.exe command syntax initially looks a bit confusing:

Defrag <volume(s)> | /C | /E <volumes> [/A | /X | /T] [/H] [/M] [/U] [/V]

Volumes represent the hard disks, while the other options are switches that indicate how you’d like the process to run. For a detailed explanation of the syntax and switch options, at the command line, type: defrag ?

The switch options are:

- A Display a fragmentation analysis report for the specified volume without defragmenting it.

- C Defragment all local volumes on the computer

- E Defragment all local volumes on the computer except those specified.

- H Run the operation at normal priority instead of the default low priority. Specify this option if a computer is not otherwise in use.

- M Run the operation on each volume in parallel. This is used primarily for SCSI or SATA type disks rather than the typical IDE.

- T Track an operation already in progress on the specified volume.

- U Print the progress of the operation on the screen.

- V Verbose mode. Provides additional detail and statistics.

- X Perform free-space consolidation. Free-space consolidation is useful if you need to shrink a volume, and it can reduce fragmentation of future files.

Windows 7 Defragmenting Example

Let’s begin by doing an analysis of your hard disk. At the prompt, type in this command:

defrag c: -a -v

Using our legend from above, this means where going to display an analysis report of the c: drive with additional details and stats.

Now that we have a report, let’s proceed with a full defragmentation. At the prompt, type in this command:

defrag c: -u

The -u switch will print the progress onscreen.

And the final output:

Going Forward

It’s important to note that in order to keep your computer running smoothly, you should defragment regularly. What that means will depend on your file activity. Some defragment weekly, but I’d say if you plan on at least monthly, that should do the trick. I hope you enjoyed this Windows 7 tutorial. And be sure to join me for future tips and tricks.

About the Author

is a writer, web developer and tech guru. Her 20 year IT career came to an end when her inner writer and entrepreneur inexplicably besieged her to give it all up. She is a self-proclaimed girl-geek and linux convert, who has held MCSE, GSEC and PMP certifications. In her dreams, she is a international best-selling sci-fi and fantasy author, but in the meantime, she now spends her days writing, managing her websites and wrecking havoc on her Ubuntu laptop.

Author's Website: http://www.veronicawrites.com


Discussion