Xylos brands

Defragmenting disks in Windows Server 2012

Checking fragmentation and defragmenting disks is still a core task for system administrators.
Everyone knows the defrag.exe utility, but it's old and you need to combine it with other tools, such as Sysinternal's psexec, to run it on a remote computer. Windows server 2012 and more specifically Powershell v3 provides this capability out-of-the-box.


Start an elevated powershell session. The command that replaces defrag.exe is called optimize-volume. If you just want to check the fragmentation of a certain volume, just type the command followed by the driveletter and Ðanalyze Ðverbose:

In this example, if we want to defrag drive C:, simply typing optimize-volume C will do the trick.


NOTE: The reason the cmdlet is called optimize-volume, is because it also performs other maintenance tasks, such as sending TRIM hints to the underlying disks if it is an SSD device. Basically, this is a way of telling the SSD about which sectors are not needed anymore and can be re-used. Other devices that can consume TRIM hints are thinly provisioned volumes, such as those on an HP 3PAR SAN, thin provisioned virtualdisk (VMDK's and VHD's) or the new Windows 2012 feature Storage spaces. They do this through the 'Unmap' command (similar to the TRIM command); this command is applicable to all kinds of storage including HDDs, SSDs, and others. By default, Windows 2012 is aware of the underlying storage, and so optimize-volume handles this automatically. You can also use the Ðretrim parameter, which generates TRIM and Unmap hints for all previously used sectors of the volume, notifying the underlying storage that the sectors are no longer needed and can be purged. This will allow for the freed up sectors to be re-used.


You can execute the analysis and defrag of disks on a remote computer by adding the ÐCimSession parameter to the command:



Depending on the size of the disk and the level of fragmentation, it can take a while to optimize a drive. If you want, you can run this task as a powershell background job, so you can continue working at your command prompt or switch to another server. Just add Ðasjob to the command.

You can then check on the progress of your optimization with the receive-job command http://technet.microsoft.com/library/hh849718.aspx

Share this blogpost

Also interesting for you

Leave a reply

Your email address will not be published. Required fields are marked.