Optimize a Drive from the CLI with defrag

Run fragmentation consolidation on HDDs or the trim pass on SSDs, with progress output.

WindowscmddiskdefragLow risk
Run `defrag C: /O /U /V` as administrator to run the correct optimization for that volume's media type.

01Command

Command
$ defrag C: /O /U /V

02What it does

The modern /O switch is media-aware: it defragments and consolidates free space on spinning disks but issues the trim pass on SSDs, so one command is correct for both. /U prints progress and /V prints verbose statistics at the end. Windows already schedules this weekly, but a manual run helps right after large file churn or before imaging a drive.

03Risk

RiskLow risk
RequirementsAdministrator privileges
!Warning: Never run defrag on a volume during heavy disk activity or on a drive you suspect is failing.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • defrag - Windows Commands
    Microsoft Learn · vendor-doc · 2026-09-17
    Evidence: Documents defrag, including /O to perform the proper optimization (trim on SSDs), /U for progress and /V for verbose output.

Last verified: 2026-09-17 · Category: System & Settings

06Related solutions