The virtual hard drive “compact” option offered in the Hyper-V menu frees little or no space. Here’s a better way.
Log into the VM and clear out any noticeable junk (temp folder, recycle bin, browser cache, incomplete downloads, etc).
Back up VM, then shut down.
In the HV, open the VM’s settings and make a note of where its vhdx is stored. Run PowerShell as an Administrator.
In PowerShell, run these commands.
Mount-VHD -Path “path to vhdx” -ReadOnly
Optimize-VHD -Path “path to vhdx” -Mode Full
(note: sometimes full mode is ineffective for some reason. If it appears to takes under 5 minutes to run a full optimization, try Quick instead.)
Dismount-VHD “path to vhdx”
Be sure to replace “path to vhdx” with the full path including drive letter.
Start up the VM and log in to make sure nothing broke.