Skip to content

Instantly share code, notes, and snippets.

@psitem
Last active November 1, 2023 17:37
Show Gist options
  • Save psitem/dd4a637d9c9449b18230485969ba3091 to your computer and use it in GitHub Desktop.
Save psitem/dd4a637d9c9449b18230485969ba3091 to your computer and use it in GitHub Desktop.
None of my Windows 2022 Server instances will automatically TRIM my SSD volumes on a schedule, so I wrote this script to run as a scheduled task.
# Simplified version runs defrag command against all drive letters with only the TRIM flag. Original version
# did not run against my Storage Spaces RAID volumes since it couldn't convert a member Physical Disk to a
# drive letter but this version does. Drive letters that Windows doesn't recognize as SSD will be skipped.
$( Get-Volume | Where-Object { [string] $_.DriveLetter -ne '' } | ForEach-Object { "$($_.DriveLetter):" } ) -join ' ' | ForEach-Object {
& defrag $($_ -split ' ') /Retrim /MultiThread
}
@psitem
Copy link
Author

psitem commented Nov 1, 2023

In retrospect, possibly just running defrag.exe /Retrim /AllVolumes /MultiThread would work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment