Home > Blockchain >  How to optimize all drives on computer using PowerShell script if computer has SSD and HDD
How to optimize all drives on computer using PowerShell script if computer has SSD and HDD

Time:10-18

Our new computers utilize SSD and old ones utilize HDD. I've been using the following script to optimize drive C:

Write-Host Optimizing Hard Drive
If((Get-PhysicalDisk | Select MediaType | where {$_.MediaType -match 'SSD'})) {
Optimize-Volume -DriveLetter C -ReTrim -Verbose
}
else {
Optimize-Volume -DriveLetter C -Defrag -Verbose
}

Now it seems like we may have computers that have both SSD and HDD. I would like to optimize all drives.

Script needs to check if disk is on the SSD or on HDD and optimize it. Then repeat for disks D and (possibly) E.

Also, script should ignore DVD drive. IDK what would happen if you would try to optimize it LOL

Please help

Thank you very much

CodePudding user response:

Try the following:

Get-Volume | 
  Where-Object DriveLetter | 
  Where-Object DriveType -eq Fixed | 
  Optimize-Volume

This tries to optimize all fixed disks that have drive letters - both HDD and SSDs - and relies on Optimize-Volume to automatically pick the right optimization strategy (-ReTrim for SSDs, -Defrag for HDDs).

From Optimize-Volume's help:

The Optimize-Volume cmdlet optimizes a volume, performing defragmentation, trim, slab consolidation, and storage tier processing. If no parameter is specified, then the default operation will be performed per the drive type as follows.

  • HDD, Fixed VHD, Storage Space. -Analyze -Defrag.
  • Tiered Storage Space. -TierOptimize.
  • SSD with TRIM support. -Retrim.
  • Storage Space (Thinly provisioned), SAN Virtual Disk (Thinly provisioned), Dynamic VHD, Differencing VHD. -Analyze - SlabConsolidate -Retrim.
  • SSD without TRIM support, Removable FAT, Unknown. No operation.
  • Related