Windows 7

How To Defrag from the Windows 7 Command Line

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.

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.

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.

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.

Ready to test your skills in Windows 7? See how they stack up with this assessment from Smarterer, the newest addition to the Pluralsight family. Start this Windows 7 test now