DEFRAG.exe – Windows CMD Command
Defragment hard drive volumes.
Syntax
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
vol_to_defrag:
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 optimization) 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:
- Volumes that the file system has locked.
- Volumes that the file system has marked as dirty, which indicates possible corruption. Run CHKDSK on a dirty volume to resolve this.
Use the fsutil dirty query command to determine if a volume is dirty.
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 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 2 am. An Administrator can change the frequency using the Optimize Drives application.
Examples:
Analyse drive C: to see if it needs to be defragmented:
DEFRAG /A C:
Defragment drive c with verbose output saved to a file:
DEFRAG c: /U /V > FileName.txt
Defragment two drives in parrallel:
DEFRAG C: D: /M
