|

How To Use The Defrag Command In Windows (2026)

The defrag command analyzes and optimizes a drive from the command line, with more options than the Optimize Drives window offers.

The basic forms are defrag C: /a to analyze a drive and defrag C: /u /v to run it with progress and detail.

This guide follows Microsoft’s defrag reference, which lists Windows 11 and Windows 10 as supported.

Table of Contents

Defrag quick answers

I want toType this
Analyze drive C without changing anythingdefrag C: /a
Defragment drive C and watch progressdefrag C: /u /v
Optimize each drive for its media typedefrag C: /o
Retrim a drivedefrag C: /l
Run on drives C and D in paralleldefrag C: D: /m
Run on every volumedefrag /c /h /v
Show helpdefrag /?

Run these in an administrator window, because Microsoft requires membership in the local Administrators group.

What the defrag command does

Microsoft says defrag locates and consolidates fragmented files on local volumes to improve performance.

Running it by hand is the same job the scheduled maintenance task does, and the graphical route is covered in how to defragment and optimize drives in Windows 11.

Open Command Prompt as administrator

  1. Press the Windows key and type cmd.
  2. Right-click Command Prompt and choose Run as administrator.
  3. Type your defrag command and press Enter.

Microsoft lists administrator membership as the minimum requirement, and suggests Run As as a security best practice.

The defrag syntax

Microsoft gives the first form as defrag <volumes> | /c | /e <volumes> [/h] [/m [n] | [/u] [v]].

Adding /a makes the same form an analysis, and adding /x makes it a free space consolidation.

The fourth form, defrag <volume> [<parameters>], is the one most people type for a single drive.

Defrag switches at a glance

SwitchWhat it does
<volume>The drive letter or mount point path to work on.
/aAnalyzes the volumes.
/bPerforms boot optimization to improve boot performance.
/cWorks on all volumes.
/dPerforms traditional defrag, which is the default.
/eWorks on all volumes except the ones you list.
/hRuns at normal priority, where the default is low.
/lPerforms retrim.
/m [n]Runs on each volume in parallel in the background.
/oPerforms the proper optimization for each media type.
/tTracks an operation already running on the volume.
/uPrints progress on screen.
/vPrints verbose output with fragmentation statistics.
/xConsolidates free space.

Microsoft also lists /g, /i and /k for storage tiers and slab consolidation, which matter mainly on server storage.

Analyze a drive first

  1. Open an elevated Command Prompt.
  2. Run defrag C: /a with your own drive letter.
  3. Read the analysis report that appears when the cursor stops blinking.

Microsoft says defrag shows a blinking cursor while it works and then prints the analysis report, the defragmentation report or both.

Defragment a drive

Microsoft’s first example is defrag c: /u /v, which defragments drive C with progress and verbose output.

If you give neither /a nor /v, Microsoft says defrag shows a summary of both reports.

To interrupt the process, press CTRL+C at the command line.

Microsoft examples

TaskCommand
Defragment drive C with progress and detaildefrag c: /u /v
Defragment C and D in parallel in the backgrounddefrag c: d: /m
Analyze a mount point and show progressdefrag c: mountpoint /a /u
Defragment all volumes at normal prioritydefrag /c /h /v

Free space needed

Microsoft says a volume needs at least 15% free space for defrag to completely and adequately defragment it.

Defrag uses that space as a sorting area for file fragments.

With less free space it only partially defragments, so delete unneeded files or move them first, as in how to free up disk space on Windows 11.

Volumes defrag cannot work on

  • Volumes locked by the file system.
  • Volumes the file system marked as dirty, which need chkdsk first.
  • Network drives and CD-ROMs.
  • Volumes that are not NTFS, ReFS, FAT or FAT32.

Microsoft says you can check whether a volume is dirty with the fsutil dirty command, and checking and repairing drive errors covers chkdsk.

Save the report to a file

Add a redirect to send the report to a text file, as in Microsoft’s defrag volume /v > FileName.txt.

What happens on SSDs

Microsoft says that when the scheduled task runs, traditional defragmentation and retrim on an SSD happen once per month.

If you manually run traditional defragmentation on an SSD between scheduled runs, the next scheduled run does analysis and retrim and skips the defragmentation.

That is why the /o switch, which picks the proper optimization for each media type, is the simplest choice when you are unsure what kind of drive you have.

The scheduled task

Microsoft says defrag runs as a scheduled maintenance task that typically runs every week, and an administrator can change how often in the Optimize Drives app.

The task may skip volumes when the computer is on battery, would need to be woken, or is resumed from idle.

Why the Last run time can look old

Microsoft explains that if analysis is skipped on a scheduled run, the Optimize Drives app does not show an updated Last run time.

As a result, the Last run time can be up to a month old even though the task is running.

Changing how often the task runs does not change the once-per-month cadence for SSDs.

Command or Optimize Drives window

NeedBetter choice
Quick look at one driveOptimize Drives window
Analyze or optimize in a script or remote sessiondefrag C: /a
Boot optimizationdefrag C: /b
Retrim onlydefrag C: /l
A saved reportdefrag C: /v > report.txt

Defrag and Disk Defragmenter conflict

Microsoft says running defrag and Disk Defragmenter at the same time is mutually exclusive.

If the graphical tool is working on a volume, the defrag command fails.

Common defrag mistakes

  • Running it without an elevated window.
  • Trying to defragment a network drive.
  • Running it on a nearly full drive and expecting a complete result.
  • Running it while Optimize Drives is already working on the same volume.

Check and optimize a drive step by step

  1. Open an elevated Command Prompt.
  2. Run defrag C: /a to analyze the drive.
  3. Run defrag C: /o to optimize it with progress shown.
  4. Read the report when the command finishes.

If the PC is still slow afterward, how to speed up a slow Windows 11 PC covers the next steps.

Official sources

The syntax, switches, requirements and examples come from Microsoft’s defrag reference.

Leave a Reply

Your email address will not be published. Required fields are marked *