Optimize VHD

Optimizes the allocation of space in or more virtual hard disk files, except for fixed virtual hard disks. The Optimization Mode operation is used to optimize the files. This operation reclaims unused blocks as well as rearranges the blocks to be more efficiently packed, which reduces the size of a virtual hard disk file. To use Optimize VHD, the virtual hard disk must not be attached or must be attached in read-only mode. The compact operation can succeed without reducing the file size, if no optimization is possible.

Input Parameters

Name Type Required Description
VHD File Path String Yes Specifies one or more paths to the dynamic or differencing virtual hard disk files to be optimized.
Optimization Mode Drop-down Yes

Specifies the mode in which the virtual hard disk is to be optimized. For a VHD disk, the default mode is Full. For a VHDX disk, the default mode is Quick. Valid modes are as follows:

  • Full scans for zero blocks and reclaims unused blocks (allowable only if the virtual hard disk is mounted read-only).
  • Pre-trimmed performs as Quick mode, but does not require the virtual hard disk to be mounted read-only. The detection of unused space is less effective than Quick mode (in which the virtual hard disk had been mounted read-only) because the scan cannot query information about free space in the NTFS file system within the virtual hard disk. Useful when the VHDX format file has been used by operating system instances that are at least Windows 8 or Windows Server 2012, or when this cmdlet has already been run on a .vhdx file in Retrim mode.
  • Pre-zeroed performs as Quick mode, but does not require the virtual hard disk to be mounted read-only. The unused space detection will be less effective than if the virtual hard disk had been mounted read-only as the scan will be unable to query information about free space in the NTFS file system within the virtual hard disk. Useful if a tool was run previously to zero all the free space on the virtual disk as this mode of compaction can then reclaim that space for subsequent block allocations. This form of compaction can also be useful in handling virtual hard disk containing file systems other than NTFS.
  • Quick reclaims unused blocks, but does not scan for zero blocks (allowable only if the virtual hard disk is mounted read-only).
  • Re-trim sends down re-trims without scanning for zero blocks or reclaiming unused blocks (allowable only if the virtual hard disk is mounted read-only).
Read Only Boolean No Specifies that the virtual hard disk is to be mounted in read-only mode.

Output Parameters

Name Type Description
Result Number Returns a success value of zero or any value other than zero to indicate failure.

Example Input Parameters

Name Type Required Example
VHD File Path String Yes c:\test\testvhdx
Optimization Mode Boolean No

Specify the mode in which the VHD is to be optimized from:

  • Full
  • Pretrimmed
  • Prezeroed
  • Quick
  • Retrim