Parallel Universe - MS Tech Blog

The ramblings of a PFE

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

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

  • Comments 1
  • Likes

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
  • 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"

    }

    }

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment