Thomas Damgaard's Blog

How to trim filesystem from PowerShell

6 March 2022 - Thomas Damgaard

In Windows 10, it is possible to trim a filesystem on an SSD by opening Properties for the drive –> Tools –> Optimize.

Windows offers a way to schedule trim to run on a regular basis. Mine is set to trim every day. And my Windows laptop is powered on all weekdays. But still, every time I check “Optimize Drives” in Windows, Current status says “XX days since last retrim.” So seemingly, the built-in scheduling does not work.

I wanted to be able to do this from a scheduled script so that I could schedule a task myself. Luckily, this is quite easy. The command to retrim a drive from PowerShell is:

 Optimize-Volume -DriveLetter C -ReTrim -Verbose

The verbose option makes it print a nice summary report:

Post Defragmentation Report:
VERBOSE:
 Volume Information:
VERBOSE:   Volume size                 = 952.28 GB
VERBOSE:   Cluster size                = 4 KB
VERBOSE:   Used space                  = 717.05 GB
VERBOSE:   Free space                  = 235.22 GB
VERBOSE:
 Retrim:
VERBOSE:   Backed allocations          = 953
VERBOSE:   Allocations trimmed         = 1849
VERBOSE:   Total space trimmed         = 237.41 GB
Filed under: powershell, tips, windows