Checking and Correcting Virtual Hard Disk Fragmentation

With both dynamically expanding and differencing virtual hard disks - the following pattern is used when writing data:

  1. Check to see if data has already been written to this area of the disk
  2. If yes - write new data to the same location as the old data
  3. If no - create new blocks on the disk, and record the new block location in the block allocation table for the virtual hard disk

Depending on the write patterns experience by a virtual hard disk - these blocks can end up being poorly aligned, or "fragmented".  You can check the fragmentation level of a virtual machines virtual hard disks by running the following command in PowerShell:

get-vm "VM Name" | Get-VMHardDiskDrive | get-vhd | Select path, FragmentationPercentage

If you believe this is causing a performance problem in your situation - there are a number of ways to defragment a virtual hard disk.  But the easiest way is to perform a storage migration - which will re-layout the blocks in a linear fashion.

Cheers,
Ben