How To Defrag from the Windows 7 Command Line

- select the contributor at the end of the page -

Troubleshooting computer problems has become a necessary component of being a computer owner. While problems are inevitable, one of the easiest ways to ward most of them off is by performing consistent and routine maintenance. One necessary aspect of your routine maintenance should include defragmenting your Windows 7 hard disk. Plan on doing it at least monthly.

Why Do I Need to Defrag Windows 7?

To understand the importance of defragmenting Windows 7, you’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 present, but the disk is mostly one contiguous block of empty space. When you install your first program, the files are placed sequentially and close to each other. However, things begin to go awry when one of a few things happen:

  1. You delete the files.

  2. You uninstall a program.

  3. You modify and append data to an existing file, which writes new data to another area of the disk.

The newly available space leaves a gap between the data that was stored before and after your deletion or modification. This space is called a fragment. In time, these fragments 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 your operations are slow.

How to Defrag Windows 7

Follow these easy steps to defrag Windows 7:

  1. Click the Start button.

  2. In the search box, type Disk Defragmenter and click on the found result.

  3. Under Current status, click the disk you want to defragment.

  4. Click Analyze disk to determine if the disk needs to be defragmented or not.

  5. If prompted, type the administrator password or provide confirmation.

  6. Once Windows is finished analyzing, check the percentage of fragmentation on the disk in the Last Run column. If the number is above 10%, the disk should be defragmented. (File fragments will always remain . . . it’s just the nature of disk storage. You will never get a perfectly defragmented hard drive.)

  7. Click Defragment disk.

  8. If prompted, type the administrator password or provide confirmation.

Disk Defragmenter might take several minutes to finish but can possibly take a few hours depending on the size and degree of fragmentation of your hard disk. However, you can still use your computer during the defragmentation process.

How to Defrag Windows 7 from the Command Line

While previous Windows versions only featured a graphical interface, Windows 7 has included a command-line tool that allows for greater control and flexibility. Here’s how to do just that:

  1. From the Start menu, click All Programs and then Accessories.

  2. Right-click on Command Prompt and select Run as Administrator.

Windows 7 Defrag Command Prompt


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 would like the process to run. For a detailed explanation of the syntax and switch options, type defrag ? at the command line.

Windows 7 Defrag switch options


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. This is useful if you need to shrink a volume, and it can reduce fragmentation of future files.

Example of Defragmenting Windows 7

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

defrag c? -a -v

Example of degragmenting windows 7


This means we are 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 the command:

defrag c: -u

The -u switch will print the progress on screen.

defrag c: -u in microsoft 7


And the final output:

Windows 7 defrag final output

What Can’t Be Defragmented in Windows 7?

There are a few situations where defragmentation won’t work:

  • If the disk is already in exclusive use by another program

  • If the disk is formatted using a file system other than NTFS file system, FAT, or FAT32

  • Network locations

  • Network drive

  • Media card

  • Flash drive

  • Optical drive

  • If the disk contains an error (which means it won’t be seen under Current status)

Learn More About How to Defrag Windows 7

If you want to learn more about how to defrag Windows 7, sign up for Windows 7 courses from Pluralsight. Or, if you’re ready to test your skills in Windows 7, see how they stack up with this assessment!


Get our content first. In your inbox.

Loading form...

If this message remains, it may be due to cookies being disabled or to an ad blocker.


Veronica Henry

Veronica Henry 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.