Hyper-V - How to compress a virtual disk with a dynamic size

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello, in this video I present how to compact a VHDX to dynamic size in Hyper-V in graphical or command line and also why sometimes it doesn’t give any result.

Code Powershell pour compacter un VHDX en taille dynamique

# VHDX location.
$vhd = "F:\vm\2019-4.vhdx"
# Read-only mount required for compaction
Mount-VHD $vhd -ReadOnly
# Volume compression
Optimize-VHD $vhd -Mode full
# Unmount the volume
Dismount-VHD $vhd
# If you do not recover any space, optimise the volume using the following options
# Directly from the virtual machine to the desired volume
Optimize-Volume -DriveLetter C -Retrim -SlabConsolidate
# Or by mounting the disk locally
# Retrieve information about the disk volumes
$Mount = Mount-VHD $vhd -Passthru | Get-Disk | Get-Partition | Get-Volume
# I filter to get only the system volume
$Volume = $Mount.Where({$_.size -gt 1gb }).path
# Optimisation
Optimize-Volume -path $Volume -Retrim -SlabConsolidate
# Unmount the volume
Dismount-VHD $vhd
# Restart the compaction operation
# Read-only mount required for compaction
$Mount = Mount-VHD $vhd -ReadOnly
# Compact the volume
Optimize-VHD $vhd -Mode full
# Unmount the volume
Dismount-VHD $vhd

Video : Hyper-V - How to compress a virtual disk with a dynamic size

Related links