Powershell Script using WMI - Check if your disk needs to be defragmented

A very simple script today just to check if the disk needs to be defragmented....

 

$getwmi = get-wmiobject win32_volume

foreach ($g in $getwmi)

{

$analysis = $g.DefragAnalysis()

if ($analsys.defragrecommended -eq $true)

{

Write-Host $g.driveletter "Needs to be defragmented!"

}

else

{

Write-Host $g.driveletter " Does not Need To Be Defragmented"

}

}

Comments

  • Anonymous
    January 01, 2003
    Adding to this... $getwmi = get-wmiobject win32_volume foreach ($g in $getwmi) { $analysis = $g.DefragAnalysis() if ($analsys.defragrecommended -eq $true) { Write-Host $g.driveletter "Needs to be defragmented!" $ans = Read-Host "Do you Want to Defrag? Press Y or N and press enter" if ($ans = "y") { start-job -ScriptBlock {$g.defrag()} } } else { Write-Host $g.driveletter " Does not Need To Be Defragmented" } }