Compact VHD Files in Windows Home

I found a great tip in GitHub to compact a VHD file without Hyper-V tools and it works great. The Dynamic VHD don't reduce their size even if the files were deleted from the hard drive, to fix it the "compact" operation is needed. This is included into the Hyper-V tools but this is only available in Windows 10/11 Pro.

There are another way to do this in Windows 10/11 Home, using the next commands:

1
2
3
4
5
6
7
8
9
diskpart

# Open window Diskpart

select vdisk file="C:\path\to\file.vhd"
attach vdisk readonly
compact vdisk
detach vdisk
exit