DEFRAG.exe (Windows 10/ Server 2012/ 2012R2/ 2016)

Defragment hard drive volumes.

      Perform analysis only:
      DEFRAGs /A  vol_to_defrag [Options]

      Defragment (default operation)
      DEFRAG [/D] vol_to_defrag [Options]

      Perform the proper optimization for each media type:
      DEFRAG /O vol_to_defrag [Options]

      Perform free space consolidation:
      DEFRAG /X vol_to_defrag [Options]

      Track an operation already in progress on a volume:
      DEFRAG /T volume


    This can be any of:

volume The drive letter or mount point path of the volume(s) to be defragmented or analyzed. To specify multiple volumes separate the names with spaces. /C Perform the operation on all volumes. /E volume_name(s) Perform the operation on all volumes Except those specified. Options: /G Optimize the storage tiers on the specified volumes. /H Run the operation at normal priority (default is low). /I n Run Tier optimization for at most n seconds on each volume. /K Perform slab consolidation on the specified volumes. /L Perform retrim on the specified volumes. /M [n] Run the operation on each volume in parallel in the background. At most n threads optimize the storage tiers in parallel. /U Print the progress of the operation on the screen. /V Print verbose output containing the fragmentation statistics.

Defrag has an undocumented option/b (boot optimisation) which will clear out the Windows\Prefetch directory. Deleting prefetch files will make the machine boot more slowly so this is not recommended.

A volume must have at least 15% free space for defrag to completely and adequately defragment it. defrag uses this space as a sorting area for file fragments.

Supported file systems: NTFS, ReFS, Fat or Fat32.

To run Defrag, you must be a member of the Administrators group on the local computer, or have been delegated the appropriate authority. When working defrag will display a blinking cursor.

On a tiered volume traditional defrag (/D) is performed only on the Capacity tier. (See FSUTIL tiering)

Some file system volumes or drives cannot be defragmented:

Volume names can be given either using the assigned drive letter (C:\) or the full volume name/path (\\?\Volume{...)

To obtain a list of volume_names using PowerShell:

PS C:\> Get-CimInstance Win32_Volume | Format-Table Name, Label, FreeSpace, Capacity

Free space consolidation (/X) takes all free sectors on the volume and arranges them together so they are contiguous. This is normally done only when you intend to re-size the volume with DISKPART SHRINK to make it smaller.
Ensure you have a good a backup before running any consolidate/shrink process as this has a tendency to fail.

Defrag's scheduled task runs as a maintenance task and by default is scheduled to run every 7 days at 2am. An Administrator can change the frequency using Optimize Drives application.


Analyse drive C: to see if it needs to be defragmented:


Defragment drive c with verbose output saved to a file:

DEFRAG c: /U /V > FileName.txt

Defragment two drives in parrallel:


“How can you expect to govern a country that has two hundred and forty-six kinds of cheese?” ~ Charles de Gaulle

Related commands:

CleanMgr - Automated cleanup of Temp files, Internet files, downloaded files, recycle bin.
DISKPART - Partition manager.
Contig - Single-file defragmenter (SysInternals/Microsoft).
pagefileconfig.vbs - PageFile Configuration.

Copyright © 1999-2021
Some rights reserved