How to Defrag Drives using Powershell in Windows Server 2012

Posted on December 20, 2012 by Jeff Hicks in PowerShell with 0 Comments

I’ve written about how you can use PowerShell to repair disk volumes, but another common server maintenance task is identifying and defragmenting drives. There are a number of enterprise solutions available on the market which might make sense for many organizations. But if you lack the budget, run a smaller shop, or prefer to do your own thing, managing this task is a little more difficult. There has been the command line tool, defrag.exe, but it needs to be combined with other techniques to handle remote systems.

With Windows Server 2012 and Windows 8, we have some new tools at our disposal for managing the defrag task using PowerShell v3. I should point out that even though you need PowerShell v3, this is operating system-specific, as it uses the new Storage module that is only available on Windows Server 2012 and Windows 8. Don’t have that? I’ll offer you an Powershell alternative later in the article.

Defrag Drives with Optimize-Volume

The new cmdlet we will use is called Optimize-Volume. It is part of the Storage module which utilizes the new CIM infrastructure. You can use this cmdlet to analyze and defrag all types of volumes.

 

Help optimize volume

We’ll focus on logical drives like C:.

How to Run an Analysis

To run an analysis all you need to do is run a command like this:

But for some reason unknown to me, the cmdlet doesn’t write any sort of result to the pipeline! Instead you have to use the –Verbose parameter.

You should get something like Figure 2:

Optimize Volume Verbose

So you can get the information you need, but it takes an extra step. If you want to save the results, you’ll need to redirect the verbose output.

On the positive side, you can specify a remote computer using –CIMSession. The remote computer must be running Windows Server 2012 or Windows 8.

You can either specify a computer name, as I’ve done here, or an existing CIM session object. This makes it (sort of) easy to analyze the same drive across multiple computers.

Sponsored

How to Defrag a Drive

When the time comes to perform the defrag, we can use the same cmdlet, but without the –Analyze parameter.

Again, I used –Verbose so I can see what happened in Figure 3.

 

Optimize Volume Novo8

Optimize-Volume will automatically perform additional operations such as trimming, depending on the drive type. So for an SSD drive the cmdlet should automatically retrim, but you can specify –Retrim if you want.

Sponsored

Defragmenting Drives: A PowerShell Alternative

Now, you could set up a scheduled task to use Optimize-Volume to defrag key volumes on your servers on a regular basis, regardless of whether they need them, but personally, I’d like a bit more control. And what if you don’t have Windows Server 2012? Either way, you’ll need an alternative.

I wrote a PowerShell function called Get-DefragAnalysis that works with PowerShell v2. You can download the Get-DefragAnalysis function. Once loaded into your PowerShell session, you can specify a drive and computers to scan.

The function writes an object like this to pipeline:

This gives you information that you can base management decisions on. It also means I can take my results and get a quick report of where I need to defrag.

You can easily defrag volumes remotely with a command like this:

 

PowerShell continues to be a blessing for IT pros, especially when it comes to managing Windows Server 2012. Granted, there are some limitations with Optimize-Volume, at least the way I would expect to use it, but it is a relatively easy way to get a handle on what volumes need to be defragged. It also handles the variety of storage types you most likely have in your environment. For everything else, I hope my Get-DefragAnalysis tool helps fill in the gaps.

 

Sponsored

Tagged with , ,