Software & OS

ScanDisk and Defrag: chkdsk and Optimize Drives in Windows

In current Windows, the job of ScanDisk is done by chkdsk and the job of defrag by the defrag command or the Optimize Drives app. Run chkdsk C: to check a drive without changing anything, chkdsk C: /f to fix errors, and defrag C: /a /v to see whether a drive needs defragmenting.

All commands below need an administrator prompt. Microsoft documents membership in the local Administrators group as the minimum for both chkdsk and defrag. Right-click Command Prompt in the Start menu and choose Run as administrator.

Which tool does what

The two old tools did two different jobs, and the modern commands keep that split:

JobCommandWhat it works on
Check a drive for file system errors and bad sectorschkdskThe file system structures and, with /r, the physical sectors
Reassemble fragmented files into contiguous piecesdefrag or Optimize DrivesFile placement on the volume
Tell an SSD which sectors are unuseddefrag /l or Optimize-Volume -ReTrimTRIM hints to the drive

Order matters. Microsoft states that defrag cannot defragment a volume the file system has marked as dirty, which indicates possible corruption, and that you must run chkdsk first. So when both are needed, check the drive first and defragment second.

How to run chkdsk (the ScanDisk replacement)

Run it with just a drive letter for a read-only check:

chkdsk C:

Without parameters, chkdsk displays only the status of the volume and does not fix any errors. If you run it without /f on an active partition, Microsoft warns it might report spurious errors because it cannot lock the drive. Treat a read-only report on your system drive as a hint, not a verdict.

To repair, add a switch:

SwitchWhat it does (per Microsoft)
/fFixes errors on the disk. The disk must be locked.
/rLocates bad sectors and recovers readable information. Includes the functionality of /f, plus analysis of physical disk errors.
/xForces the volume to dismount first if necessary. All open handles to the drive are invalidated. Includes /f.
/scanNTFS only. Runs an online scan of the volume.
/spotfixNTFS only. Runs spot fixing on the volume.
/offlinescanandfixRuns an offline scan and fix on the volume.
/bNTFS only. Clears the list of bad clusters and rescans all allocated and free clusters. Includes /r. Meant for use after imaging a volume to a new drive.

The example in Microsoft's documentation is:

chkdsk D: /f

You cannot open files on that drive until chkdsk finishes.

What "the volume is in use" means

If you use /f on a drive that has open files, such as your Windows drive, chkdsk cannot lock it and prints this message:

Chkdsk cannot run because the volume is in use by another process. Would you like to schedule this volume to be checked the next time the system restarts? (Y/N)

Answer Y and restart. According to Microsoft, chkdsk then checks the drive and corrects errors automatically at restart, and if it is the boot partition it restarts the computer again after the check. Alternatively, /x forces a dismount, but that invalidates every open handle to the drive, so save your work and close programs that use it.

How to read the result

chkdsk reports an exit code that scripts can test (in Command Prompt, echo %errorlevel% right after it finishes):

Exit codeMeaning
0No errors were found.
1Errors were found and fixed.
2Performed disk cleanup (such as garbage collection) or did not perform cleanup because /f was not specified.
3Could not check the disk, errors could not be fixed, or errors were not fixed because /f was not specified.

When a check runs at startup you never see its output on screen for long. Read the log afterwards in PowerShell, using Microsoft's documented command:

get-winevent -FilterHashTable @{logname="Application"} | ?{$_.providername -match "wininit"} | fl timecreated, message

Use "chkdsk" instead of "wininit" for scans that ran while Windows was up. In Event Viewer, the same entries are under Windows Logs, Application, filtered to the sources Chkdsk and Wininit.

How long chkdsk takes, and the SSD caveat

Microsoft notes that /r and /b read every sector, so on a large hard disk they can take a considerable amount of time, and runtimes of many hours are not unusual. SSDs read faster because there is no seek time. Microsoft also cautions that repeated full-surface scans with /r on an SSD could unnecessarily increase write/erase cycles and slightly reduce its lifespan, though for occasional checks this is not a significant concern. Use plain chkdsk /f unless you have a reason to suspect bad sectors.

How to run defrag

Start with analysis, which changes nothing:

defrag C: /a /v

/a analyzes the volume and /v prints verbose output with the fragmentation statistics. To defragment with progress and verbose output, Microsoft's own example is:

defrag C: /u /v

The main switches:

SwitchMeaning
/aAnalyze only.
/dTraditional defrag. This is the default.
/oPerforms the proper optimization for each media type.
/lPerforms retrim on the specified volumes.
/xFree space consolidation.
/cOperates on all volumes.
/hRuns at normal priority. The default is low.
/uPrints the progress on screen.

Three limits are worth knowing. A volume needs at least 15% free space for a complete defragmentation, because defrag uses that space to sort file fragments; below that it only partly defragments. It cannot defragment network drives, CD-ROMs, volumes locked by the file system, or volumes that are not NTFS, ReFS, FAT or FAT32. And you can interrupt it with Ctrl+C.

Command-line defrag and the graphical Disk defragmenter exclude each other. If one is running on a volume, the other fails or has its options disabled.

Optimize Drives, the scheduled task and SSDs

Windows runs defragmentation as a weekly maintenance task by default, and an administrator can change how often in the Optimize Drives app. For SSDs, Microsoft documents a different policy: the scheduled task does traditional defragmentation and retrim only once per month, whatever frequency you set. If you manually run traditional defragmentation on an SSD between scheduled runs, the next scheduled run does analysis and retrim and skips the defragmentation.

The scheduled task may also skip drives. Microsoft lists reasons: it will not wake the computer, it does not run on battery power, and it does not run when the computer has just resumed from idle. That is why the Last run time in Optimize Drives can look stale on a laptop.

The PowerShell equivalent

The Storage module has Optimize-Volume. With no switches it chooses an operation by drive type, per Microsoft: analyze plus defrag on an HDD, retrim on an SSD with TRIM support, and no operation on an SSD without TRIM support. The explicit forms are:

Optimize-Volume -DriveLetter C -Analyze -Verbose
Optimize-Volume -DriveLetter C -Defrag -Verbose
Optimize-Volume -DriveLetter C -ReTrim -Verbose

The first only reports the current state, the second defragments, and the third sends TRIM hints for all unused sectors. -NormalPriority raises the priority from the default of low. For the volume-repair counterpart, the same documentation lists Repair-Volume as a related cmdlet; this article does not cover its syntax.

If it still fails

  • defrag refuses to start: check whether the volume is dirty with fsutil dirty query C:, then run chkdsk C: /f and restart.
  • defrag says it ran but fragmentation is high: check free space. Under 15% free, it only partly defragments.
  • chkdsk finishes suspiciously fast on a large drive: Microsoft says it may have only done an online scan of the file system structure. Use /offlinescanandfix or schedule a check at restart if you need an offline pass.
  • Repeated errors after each repair: a drive that keeps returning errors may be failing. Back up your files first, because chkdsk repairs on FAT volumes in particular can sometimes cause a loss of data.

Frequently asked questions

Is there still a ScanDisk in Windows 10 and 11?
Microsoft's command reference documents chkdsk as the command that checks the file system and file system metadata of a volume for logical and physical errors. That is the job people mean when they search for ScanDisk. For defragmenting, the defrag command and the Optimize Drives app are documented.
Should I defragment an SSD?
Not by hand as a routine. Microsoft documents that the scheduled task runs traditional defragmentation and retrim on SSDs once per month, and that Optimize-Volume with no switches does only a retrim on an SSD with TRIM support. Leave the schedule on its default.
Why does defrag say it can only partly defragment my drive?
Microsoft documents that a volume needs at least 15% free space for defrag to defragment it completely, because it uses that space as a sorting area. With less free space it only partially defragments. Delete or move files to free space.
Can I stop chkdsk once it has started?
Microsoft does not recommend interrupting it, but says that doing so should not leave the volume more corrupt than before. Running chkdsk again should repair what remains.

Sources