Last active
June 13, 2025 03:56
-
-
Save Trass3r/435b4647a721c9f52c5f8e02d0a63dac to your computer and use it in GitHub Desktop.
Reduces disk space taken by the WSL and Docker vhdx files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ErrorActionPreference = "Stop" | |
| $files = Get-ChildItem -Path C:\Users -Recurse -Include "ext4.vhdx" -Force -ErrorAction SilentlyContinue | Select-Object FullName, Length | |
| wsl -u root fstrim / | |
| foreach ($file in $files) { | |
| echo "Compacting $file" | |
| wsl --shutdown | |
| Mount-VHD -Path $file.FullName -ReadOnly | |
| Optimize-VHD -Path $file.FullName -Mode Full | |
| Dismount-VHD $file.FullName | |
| } | |
| Get-ChildItem -Path C:\Users -Recurse -Include "ext4.vhdx" -Force -ErrorAction SilentlyContinue | Select-Object FullName, Length |
Author
Yeah you can install them manually even on Home editions if you search for smth like "windows home hyperv install".
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some windows editions, you don't have the VHD commandlets, instead you can do:
From microsoft/WSL#4699 (comment)