Remotely defrag Windows 2003 and 2008

I needed to defrag some harddisks the other day and started logging into them to start the defrag process. I didnt like the idea of my credentials being open on a console when I wasnt there, but I didnt know of any better process (outside of purchasing 3rd party products).

A little googling turned up the following. Its a poweshell script that analyzes disk fragementation, and then launches defrag on the remote system. Original source:

$servers=”SERVER1″, “SERVER2”

foreach( $server in $servers){
Write-Host “”
$v=(gwmi win32_volume -computer $server)
“CURRENT SERVER: {0} ” -f $server
“NUMBER VOLUMES: {0} ” -f $v.length
foreach( $volume in $v){
Write-Host “”
write-host “Analyzing fragmentation on” ($volume.DriveLetter) “…”
if ($frag -gt “10”) {
write-host “Drive” ($volume.DriveLetter) “is currently” $frag “% fragmented.” -foreground RED
write-host “Starting remote defrag…”
else {
Write-host “Drive” ($volume.DriveLetter) “is not fragmented” -foreground GREEN
Write-Host “”

Leave a Reply