-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptimizeWSLdisk.ps1
34 lines (29 loc) · 1.08 KB
/
OptimizeWSLdisk.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Shutdown WSL and kill WSL service
wsl --shutdown
Stop-Service -Name "LxssManager" -Force -ErrorAction SilentlyContinue
Stop-Service -Name "wslservice" -Force -ErrorAction SilentlyContinue
# List WSL distributions
$wslDistros = wsl --list --quiet
foreach ($distro in $wslDistros) {
try {
# Retrieve the location of the ext4.vhdx file
$vhdxPath = (Get-ChildItem -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | Where-Object { $_.GetValue("DistributionName") -eq $distro }).GetValue("BasePath") + "\ext4.vhdx"
if ($vhdxPath) {
# Create a diskpart script to compact the vdisk
$diskpartScript = @"
select vdisk file="$vhdxPath"
attach vdisk readonly
compact vdisk
detach vdisk
exit
"@
# Execute the diskpart script
$diskpartScript | diskpart
}
} catch {
Write-Host "Fail to locate virtual disk of: " + $distro
}
}
# Restart WSL service
Start-Service -Name "LxssManager" -ErrorAction SilentlyContinue
Start-Service -Name "wslservice" -ErrorAction SilentlyContinue