Windows

Exert control and defrag from the command line in Windows 7

Using the options available from the command line version of the Windows 7 defragmentation utility you have much more control over the maintenance of your storage devices.

Of all the maintenance tasks one must do to keep a personal computer running properly, none is as basic as defragmentation. The constant saving and deleting of files can cause major inefficiencies in the way your storage hardware performs. Running the Windows 7 defragmentation utility can restore some of that lost efficiency, but if you use the command line you can expand your options and enhance your control over the process.

Note: I saw this tip on TechNet but I wanted to expand on it a little bit. As they mention, the syntax for the Windows 7 version of the command line version of defrag.exe is different than it was for Vista.

Syntax

The defrag.exe application should be run from an elevated command prompt. Click the Start button and type cmd in the Search box. Navigate to the cmd.exe menu item and right-click it to get to the context menu where you should click the Run as administrator entry as shown in Figure A,

Figure A

Run an elevated command line prompt
After answering the UAC, you will get the familiar command prompt window, similar to Figure B.

Figure B

Run defrag.exe from here

Here is the general syntax for defrag.exe:

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

And here is an explanation for each of the available options:

Switch

Description

/C

Defragment all local volumes on the computer.

/E

Defragment all local volumes on the computer except those specified.

/A

Display a fragmentation analysis report for the specified volume without defragmenting it. This option will tell you whether you actually need to defrag a particular drive.

/X

Perform free-space consolidation on a particular volume. This option will delete some system restore points and other extraneous files and frees that space for use.

/T

Track an operation already in progress on the specified volume. This option will report whether a defrag operation is currently taking place on a particular volume.

/H

Run the operation at normal priority instead of the default low priority. Specify this option if a computer is not otherwise in use. This option should increase the speed at which a defrag operation is performed.

/M

Defragment multiple volumes simultaneously, in parallel. You will need to have SATA or SCSI drives for this to be useful.

/U

Print the progress of the operation on the screen. This is a good option to have on so you can track your commands - without it you may find yourself wondering if anything is happening.

/V

Verbose mode. Provides additional detail and statistics, which will help your decision making, especially when coupled with the analysis option listed above.

<volume>

The drive letter or mount point of the volume to defragment.

Bottom line

The various defrag options give you a significant amount of control with regard to this common utility. Probably the most common command with this technique would be:

Defrag C: /H /U /V
That would give the defrag operation for drive C: higher priority, show you the progress, and provide the most description during each step. (Figure C)

Figure C

Defrag complete

Additional information:

About

Mark Kaelin is a CBS Interactive Senior Editor for TechRepublic. He is the host for the Microsoft Windows and Office blog, the Google in the Enterprise blog, the Five Apps blog and the Big Data Analytics blog.

6 comments
roy.gernon@LA.GOV
roy.gernon@LA.GOV

MyDefrag is a free open source defragger that has preset config scripts for system (boot) drive or data drive for daily, weekly, and monthly defrags. The monthly system drive defrag script does the most work and optimization. It looks at the info Windows collects to optimaze boot time and frequent app use, and puts those files on the fastest part of the disk. It creates 6 or so Zones with buffer space between, and the large media data goes to the slower end of the disk. Your slow part of the drive can still read the media 5 times faster than you play it in real time. I did this instead of fresh re-install of XP, and my weekly Anti-Virus Scan improved its run time by 33%! MyDefrag does not have auto-defrag.

sakkar
sakkar

I use Diskeeper too. (The latest version with the intelliwrite) Has worked very well and is reliable. Since I set it to defrag in the background, I've not had any hassles with incomplete or interrupted defrags.

Spexi
Spexi

In Windows 7 there is now a premade task for taking care of defrag the C: drive/partition/volume on regular basis. Why not take a look on that one as everyone has it from the start when the system is shining new. I guess many of these commands might be possible put inside these settings.... Myself using Mydefrag (formely JkDefrag) Many of these third party programs having features for move e.g systemfiles to the beginning of disk surface which can increase the speed in how Windows will work when searching and reading from the diskdrive. If consider how little and tiny the bootpartition normally is I doubt there lay much to winn in the long run by defrag these small files. The most important is to have them in beginning of disk, which they also normally are when install a new system on a fresh clean disk.

ole.handeland
ole.handeland

Type defrag c: -b to defrag the boot sector of the harddisk. The sector is omitted in a "normal" defrag. My experience is that this makes a difference in starting up the computer.

Mark W. Kaelin
Mark W. Kaelin

Do you prefer third-party defrag tools? What are the benefits of using the command line for defragging?

byl
byl

Very thorough information -- my commendations! To answer your question, yes, I prefer third party defrag tools as using the built-in can take a very long time (and you really can't use the computer while it is running). Here is a Top 10 Reviews side-by-side of the best defrag programs available: http://disk-defragmenter-software-review.toptenreviews.com/ The top placers are all automatic and the top 3 can defrag while the PC is in use. The top placer won because it was undetectable during defrag despite having several programs open while using the PC and it also prevents fragmentation. Most of the programs tested offer free trials so you can try before you buy (you can get a free trial of the top placer at http://www.diskeeper.com/defrag ) ..

Editor's Picks