Disk Defragmentation

This script defragments all local disks. Regular defragmentation can improve disk performance, particularly on HDDs.


# Defragment all local disks
$drives = Get-WmiObject -Class Win32_Volume | Where-Object { $_.DriveType -eq 3 }  # 3 = Local Disk

foreach ($drive in $drives) {
    Write-Output "Defragmenting drive $($drive.DriveLetter)"
    Start-Process -FilePath "defrag.exe" -ArgumentList "$($drive.DriveLetter) /O /V" -NoNewWindow -Wait
}

• Explanation: This script finds all local disks and defragments them using defrag.exe. The /O option optimizes SSDs as well, and /V provides detailed output.

Last updated