Skip to content

Instantly share code, notes, and snippets.

@renshuki
Created September 4, 2025 14:43
Show Gist options
  • Save renshuki/39b6f32df69c27ae0938681076782180 to your computer and use it in GitHub Desktop.
Save renshuki/39b6f32df69c27ae0938681076782180 to your computer and use it in GitHub Desktop.
Windows PowerShell script to compact WSL vdisk (and recover unused disk space)
# Shut down WSL
wsl --shutdown
# Define the diskpart commands
$diskpartCommands = @"
select vdisk file="C:\path\to\image.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
"@
# Run diskpart with the commands
$diskpartCommands | diskpart
Write-Output "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment