Scripting Defrag in Windows Server 2003

Use "Defrag" and "DefragAnalysis" to manage the Defrag utility included with Windows Server 2003.

Methods named "Defrag" and "DefragAnalysis" (part of Win32_Volume) are used to manage the Defrag utility included

with Windows Server 2003.

Microsoft TechNet Script Center provides a nice example for Analyze Volume Defragmentation. The Technet Script Center Repository also has an example for how to defrag a Windows Server 2003 system using WMI:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
Set colVolumes = objWMIService.ExecQuery _
("Select * from Win32_Volume Where Name = 'D:\'")
For Each objVolume in colVolumes
errResult = objVolume.Defrag()
Next

Using this information, I created a script to enumerate all local hard drives and to check each one sequentially to see if it needs to be defragmented (TotalPercentFragmentation > 10%). If so, run Defrag. All information is written to the event log.

If you have any questions, you can e-mail the author of this tip at ramseyg@hotmail.com.

ABOUT THE AUTHOR: Greg Ramsey has been working with SMS and desktop deployment since 1998. He currently works for a state agency in Columbus, Ohio. He has extensive knowledge in VBScript (including ADSI, WSH and WMI), Visual Basic, SMS Installer and good 'ol batch files. He is also the team lead for creating and managing both unattended builds and image-based installations using SysPrep.


This article first appeared in myITforum, the premier online destination for IT professionals responsible for managing their corporations' Microsoft Windows systems. The centerpiece of myITforum.com is a collection of member forums where IT professionals exchange technical tips, share their expertise and download utilities that help them better manage their Windows environments, specifically Microsoft Systems Management Server (SMS). To register for the site and sign up for the myITforum daily newsletter, click here.

This was first published in August 2004

Dig deeper on Microsoft Group Policy Management

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close