PowerShell – Defragment

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.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s