Tuesday, 9 June 2009

Defrag a disk with PS…

At various points during the Config Manager course I was on last week there were some inevitable lulls to proceedings whilst waiting for the virtual machines to do their thing.  To fill the time I revisited the Win32 classes documentation on MSDN (okay, surely I could have found something more interesting to browse, but once a geek always a geek…).  There I came across a class that I hadn’t spotted before: Win32_DefragAnalysis.  Straight away I started to think “hmm, can I check a disk and then defrag it if required using a PS script…?”.  Granted, any normal person would have thought “Hmmm, boring.  Maybe I’ll just start an InPrivate browsing session and find something more interesting…”, but I digress.  It turns out that it’s quite easy, use Win32_Volume to identify what drive you want to analyse/defrag then use that to hook into Win32_DefragAnalysis/Win32_Defrag…

image

Win32_DefragAnalysis & Win32_Defrag are only available on Server 2003 and higher.  I tested this on Win7 and it ran fine, but it needs to be run elevated.

Will I ever need to use a script to defrag a drive?  Probably not, but it’s nice knowing that I could if I wanted to…

No comments:

Post a Comment