Skip to content

Instantly share code, notes, and snippets.

@MrDwarf7
Forked from Csqhi515/CompactWSL.bat
Created November 18, 2024 01:53
Show Gist options
  • Save MrDwarf7/6d10f3b4e09e6e90702a917a3a64fd67 to your computer and use it in GitHub Desktop.
Save MrDwarf7/6d10f3b4e09e6e90702a917a3a64fd67 to your computer and use it in GitHub Desktop.
Script to help compact WSL and vhdx.
@echo off
net session >nul 2>&1
if %errorlevel% neq 0 (
echo Requesting administrative privileges...
powershell -Command "Start-Process '%~f0' -Verb RunAs"
exit /b
)
wsl sudo fstrim --all; echo "Exit status: $?";
wsl --shutdown
:: ========= Instructions =========
:: Please define your list of files here.
set fileList=("%LOCALAPPDATA%\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx" "%LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx")
:: Process each file in the list.
for %%F in %fileList% do (
echo Processing %%F using diskpart compact.
(
echo select vdisk file=%%F
echo detach vdisk
echo attach vdisk readonly
echo compact vdisk
echo detach vdisk
) | diskpart
)
echo Finished.
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment