MS-DOS and Windows command line defrag command

Updated: 12/30/2021 by Computer Hope
defrag command

Microsoft Defrag is a software utility capable of organizing and optimizing the files on the hard drive to improve system performance. It was first introduced with MS-DOS 6.0.

Availability

Defrag is an external command available for the following Microsoft operating systems.

  • MS-DOS 6.0 and above and all versions of Windows.

Defrag syntax

Windows 10 and Windows 11 syntax

Defrag <Volumes> <Operations> [<Options>]

<Volumes> parameters

/C On each volume, run only the preferred operations from the given list of operations.
/E <volume paths> Perform all the given operations on each volume, except those specified. If the exception list is empty, this behaves as /C.
volume paths Specifies the drive letter followed by a colon, mount point, or volume name. More than one volume can be specified. Run all the given operations on each specified volume.

<Operations> parameters

/A Perform analysis on the specified volumes.
/B Perform boot optimization to increase boot performance.
/D Perform traditional defrag (this is the default). On a tiered volume, traditional defrag is performed only on the Capacity tier.
/G On tiered volumes, optimize files to reside on the appropriate storage tier.
/K On thinly provisioned volumes, perform slab consolidation to increase slab usage efficiency.
/L On thinly provisioned volumes, perform retrim to release free slabs. On SSDs, perform retrim to improve write performance.
/O Perform the proper optimization for each media type.
/T Track the progress of a running operation for a given volume. An instance can show progress only for a single volume. To see progress for another volume, launch another instance.
/U Print the progress of the operation on the screen.
/V Print verbose output containing the fragmentation statistics.
/X Perform free space consolidation, moves free space towards the end of the volume (even on thin-provisioned volumes). On tiered volumes, consolidation is performed only on the Capacity tier.

<Options> parameters

/H Run the operating at normal priority (default is low).
/I n Available only with /G (/TierOptimize) Operations parameter. Tier optimization would run for at most n seconds on each volume.
/LayoutFile <file path> Available only with /B (/BootOptimize) Operations parameter. The specified file contains the list of files to be optimized. The default location is %windir%\Prefetch\layout.ini.
/M [n] Run the operation on each volume in parallel in the background. For /G (/TierOptimize), at most n threads optimize the storage tiers in parallel. The default value of n is 8. All other optimizations ignore n.
/OnlyPreferred When volumes are specified explicitly, defrag performs all the given operations on each specified volume. This switch lets defrag run only the preferred operations on each specified volume from the given list of operations.

Windows Vista and later syntax

DEFRAG <volumes> | /C | /E <volumes> [<task(s)>] [/H] [/M | [/U] [/V]]

Where <task(s)> is omitted (traditional defrag), or as follows:

/A | [/D] [/K] [/L] | /O | /X

Or, to track an operation already in progress on a volume:

defrag <volume> /T

Parameters:

/A Perform analysis on the specified volumes.
/C Perform the operation on all volumes.
/D Perform traditional defrag (this is the default).
/E Perform the operation on all volumes except those specified.
/H Run the operation at normal priority (default is low).
/K Perform slab consolidation on the specified volumes.
/L Perform retrim on the specified volumes.
/M Run the operation on each volume in parallel in the background.
/O Perform the proper optimization for each media type.
/T Track an operation already in progress on the specified volume.
/U Print the progress of the operation on the screen.
/V Print verbose output containing the fragmentation statistics.
/X Perform free space consolidation on the specified volumes.

Windows XP and earlier syntax

defrag volume
defrag volume [/a]
defrag volume [/a] [/v]
defrag volume [/v]
defrag volume [/f]
/A Analyzes the volume and displays a summary of the analysis report.
/V Displays the complete analysis and defragmentation reports.

When used in combination with /a, displays only the analysis report. When used alone, displays both the analysis and defragmentation reports.

/F Forces defragmentation of the volume regardless of whether it needs to be defragmented.

MS-DOS 6.0 and earlier syntax

Below is a listing of available switches in MS-DOS Defrag 6.0 and above. Note that these commands will not work in Windows 95 and above, and defrag should not run while Windows is running in the background.

DEFRAG Drive: /F /U /S:order /B /Skiphigh /LCD /BW /G0 /A /H
Drive: Drive letter that you want to defragment.
/F Ensures that no empty space remains between files.
/U Leaves empty space if any is found between files.
/S:order Sorts files in specific sort <order>.
N = Alphabetic name order.
-N = Reverse alphabetic name order.
E = In alphabetic file extension order.
-E = Reverse alphabetic file extension order.
D = In date order (earliest to latest).
-D = Reverse date order (old to new).
S = By file size (small to large)
-S = By file size (large to small)
/B Reboot the computer after completing defrag.
/Skiphigh Load defrag into conventional memory.
/LCD Start defrag in LCD color mode.
/BW Start defrag in black and white color mode.
/G0 Disable mouse and character set.
/A Start defrag in automatic mode.
/H Move hidden files.

Defrag examples

defrag c:

Defrag the main hard drive and correct any fragmented files.