Defragment your hard disk from the Command Prompt

In our previous article we mentioned that the Disk Defragmenter has been simplified a lot. In this new version, the user interface doesn’t allow users to select only one of their partitions for defragmentation. You can defragment all your partitions at once or none. However, this doesn’t mean that users they cannot customize the Disk Defragmenter at all.

You can always start the Command Prompt and use the appropriate command line parameters to obtain the results you want.

When you start the Command Prompt, it is better to right click on its shortcut and select the Run as administrator option.

Command Prompt

Once the Command Prompt is started, type “defrag” or “defrag.exe” plus the appropriate parameters and Disk Defragmenter will defragment or analyze the partition you want.

The list of supported command line parameters for “defrag.exe” is the following:

  • drive letter – this parameter specifies the drive letter or the mount point path of the volume that will be analyzed or defragmented. (e.g. “defrag c:” or “defrag e:\volume\mountpoint”);
  • -c – using this parameters forces “defrag.exe” to defragment all volumes on your computer. (e.g. “defrag -c”);
  • -a – performs only fragmentation analysis and it doesn’t defragment the specified drive. (e.g. “defrag d: -a”);
  • -r – performs a partial defragmentation which consolidates only the fragments which are smaller than 64 MB. This is the default setting. This means that “defrag v:” and “defrag v: -r” are equivalent and the Disk Defragmenter will perform the same type of defragmentation;
  • -w – when you use this switch, “defrag.exe” will perform a full defragmentation, regardless of the fragments size. (e.g. “defrag c: -w”);
  • -f – forces the defragmentation even if the free space is lower than 15%. (e.g. “defrag c: -f”);
  • -v – verbose mode. In this mode, “defrag.exe” will show a detailed analysis and defragmentation output. (e.g. “defrag d: -v”);
  • -? – displays help information about how to use “defrag.exe”. (e.g. “defrag -?”).

The switches can be combined to obtain the desired result. You just need to respect the following syntax:
defrag volume -a [-v]
defrag volume [{-r | -w}] [-f] [-v]
defrag -c [{-r | -w}] [-f] [-v]

Here are some examples, to help you understand better:

    • defrag –c –w –f –v – defragments all you hard drives and all files regardless of files & fragments size, even if there is less than 15% of hard drive space left and shows a detailed analysis and defragmentation report;
    • defrag d: -w -f – makes a full defragmentation of your “D:” drive, even if the space available is lower than 15%.

Related articles:
Disk Defragmenter
What is new in the Disk Defragmenter from Windows Vista SP1
Benchmarking the Disk Defragmenter
Disk Cleanup

Recommended articles:
Add Defragment to the Right-Click Menu for a Drive
Disk Defragmenter FAQ

6 thoughts on “Defragment your hard disk from the Command Prompt”

    • riiiiight
      Safe? Just run a chkdsk /f and reboot before defrag.

      Many defrag tools out there are as limited as ‘defrag’.
      But some are excellent.

      Contig (Microsoft Freeware) allow us to defrag any specific files.

      UltraDefrag (Open Free Software) also allows us to defrag all
      unmovable locked files when Windows boots. And -o and
      set UD_IN_FILTER=pagefile;.evt;ntuser;System32config
      moves the most frequently used files to the fastest area of
      our Drive, the beginning.

  1. Verbose real-time?
    If choosing verbose mode, do details come as they occur (like in the old Defragmenter where you could “watch it work”), or is it a detailed analysis that pops up all at once when the defrag is done???

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.