Timeline

2025-12-29

init


Locate the .vhdx File Disk Path

Replace distribution-name with the distribution name shown by wsl -l -v:

1
2
3
4
5
6
wsl -l -v

(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq '<distribution-name>' }).GetValue("BasePath") + "\ext4.vhdx"

# Example for archlinux
(Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq 'archlinux' }).GetValue("BasePath") + "\ext4.vhdx"

Compress the Disk

Shut down WSL before compressing:

1
wsl --shutdown

Then launch diskpart:

1
diskpart

A new command line window will pop up. Enter the following commands in order, replacing the file value with the path obtained in the previous step:

1
2
3
4
5
6
7
8
9
10
# Select the target disk file
select vdisk file="/path/to/your/.vhdx"
# Attach the virtual disk file in read-only mode
attach vdisk readonly
# Compact the virtual disk file
compact vdisk
# Detach the virtual disk file
detach vdisk
# Exit
exit

References