Skip to content

How-ToHDD

How to Defragment a Hard Drive in Windows 11 With the defrag Command

Still using a hard drive for games or media? Windows 11's built-in defrag command can analyse and optimise it. Following Microsoft's documentation, we show how to analyse a drive, run a defrag, let Windows choose the right optimisation for HDDs and SSDs, and what to do if a volume won't defragment.

By , Systems & Technical Editor

2 min read

How to Defragment a Hard Drive in Windows 11 With the defrag Command

If you still use a spinning hard drive for games, media or backups, Windows 11 includes a built-in defrag command that can analyse and optimise it. This guide follows Microsoft's official documentation for the command.

What defrag does

Microsoft Learn says the command "locates and consolidates fragmented files on local volumes to improve system performance". It applies to Windows 11 and Windows 10 as well as Windows Server, and you need to be a member of the local Administrators group, or equivalent, to run it.

Step 1: Open a terminal as administrator

Open Terminal, PowerShell or Command Prompt with administrator rights (run as administrator), because Microsoft says membership of the local Administrators group, or equivalent, is the minimum required to run the command.

Step 2: Analyse the drive first

To check a drive before optimising it, run defrag with the /a parameter, which Microsoft says "performs an analysis on the specified volumes". Add /v for verbose output with fragmentation statistics:

defrag D: /a /v

Replace D: with your hard drive's letter.

Step 3: Defragment the drive

Running defrag against a volume performs a traditional defrag, which Microsoft lists as the default. Add /u to see progress on screen and /v for verbose output:

defrag D: /u /v

By default the operation runs at low priority; Microsoft says /h runs it at normal priority instead. Other useful switches include /x for free space consolidation and /c to work on all volumes.

Let Windows pick the right job: /o

If you have a mix of hard drives and SSDs, the /o parameter "performs the proper optimization for each media type", so you don't have to work out which drive needs which treatment:

defrag /c /o

What about SSDs?

For SSDs the job is different. The defrag command's /l parameter "performs retrim on the specified volumes", which is the SSD-appropriate job. Our guide on how to optimise SSD TRIM in Windows 11 explains TRIM in more detail.

When defrag won't run

Microsoft lists volumes that can't be defragmented, including volumes locked by the file system, network drives, CD-ROMs and file systems other than NTFS, ReFS, FAT or FAT32. If the file system has marked a volume as dirty, which indicates possible corruption, Microsoft says you must run chkdsk before you can defragment it. You can check whether a volume is dirty with the fsutil dirty command. See how to check disk errors with chkdsk in Windows 11.

The bottom line

For a hard drive, open an administrator terminal, analyse it with defrag D: /a /v and, if it needs work, run defrag D: /u /v, or simply use defrag /c /o so Windows chooses the right optimisation for every drive. For SSDs, use /o or the /l option, which performs retrim, and if Windows refuses because a volume is marked dirty, run chkdsk first.

Sources

  1. 01Microsoft Learn: defrag

About the author

Marcus Thorne

Systems & Technical Editor

Marcus writes Forge PC's step-by-step build, troubleshooting and system optimisation guides. His background spans Windows kernel and power-state diagnostics, UEFI/BIOS configuration, NVMe storage arrays, ATX 3.1 power delivery and running local AI and LLM workloads on consumer graphics cards.

More from Marcus Thorne

Read next

NVCleanstall vs DDU: Which Graphics Driver Tool Do You Need?

Versus

NVCleanstall vs DDU: Which Graphics Driver Tool Do You Need?

NVCleanstall and DDU are often mentioned together, but they do opposite jobs. Based on the official TechPowerUp and Wagnardsoft pages, we explain how NVCleanstall trims an NVIDIA driver install, how DDU removes AMD, NVIDIA and Intel drivers and their leftovers, and when to use each or both.

Callum Reid2 min

HWiNFO vs GPU-Z: Which Free Hardware Info Tool Do You Need?

Versus

HWiNFO vs GPU-Z: Which Free Hardware Info Tool Do You Need?

HWiNFO and GPU-Z are both free hardware information tools, but they're built for different jobs. Using the developers' official pages, we compare HWiNFO's whole-system monitoring and reporting with GPU-Z's focused graphics card details, BIOS backup and PCIe lane check, and explain when to use each.

Callum Reid2 min