Geeks With Blogs

Make IT Easy

Windows Server 2008 R2 has a built in Scheduled Task that defragments your hard drive sometime over night. But, I was wondering if this is actually working, and on one server I have found it isn't.

So, here is my PS script that I'm working on. This is also my first "real" PowerShell script I've written. It took me some time!

foreach ($computerName in Get-Content "C:\Users\matt.ladd\Desktop\Scripts\Servers.txt") 


Write-Host $computerName $drive = Get-WmiObject -computerName $computerName -Class Win32_Volume -Filter "DriveLetter = 'C:'"

$report = $drive.DefragAnalysis()

$frag = $report.DefragAnalysis

$frag | Select-Object FilePercentFragmentation | Format-List


This will return the Fragmentation percentage as well as the computer name.

Still working on a text file with server names to read from. Also, the plan is to send that to myself once a week in a .csv format.

Posted on Tuesday, August 9, 2011 7:34 AM | Back to top

Comments on this post: PowerShell - Defragment

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Make IT Easy | Powered by: