Skip to content

Instantly share code, notes, and snippets.

@Csqhi515
Created October 18, 2024 17:35
Show Gist options
  • Save Csqhi515/7d62d9ba2f94e1f7a762e9e8a8429d55 to your computer and use it in GitHub Desktop.
Save Csqhi515/7d62d9ba2f94e1f7a762e9e8a8429d55 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