Skip to content

Commit e4b174e

Browse files
committed
WSL mgmt script
1 parent 722f5b9 commit e4b174e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

devops/utils/compact_wsl_disks.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This script will search for WSL's VHDX files for and compact them
2+
3+
# ENSURE:
4+
# 1. You can run scripts:
5+
# Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
6+
# 2. You have Hyper-V management tools:
7+
# Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All -All -NoRestart
8+
9+
10+
# Define the base folder path using the LOCALAPPDATA environment variable
11+
$baseFolderPath = Join-Path -Path $env:LOCALAPPDATA -ChildPath "Packages"
12+
13+
# Search for all VHDX files matching the pattern
14+
$vdiskFiles = Get-ChildItem -Path $baseFolderPath -Recurse -Filter "ext4.vhdx"
15+
16+
Write-Host "Shutting down WSL..."
17+
wsl --shutdown
18+
Start-Sleep -Seconds 5 # Wait a few seconds to ensure WSL is fully shut down
19+
Write-Host "WSL shut down successfully."
20+
21+
# Loop through each VHDX file found
22+
foreach ($vdisk in $vdiskFiles) {
23+
Write-Host "Processing VHD/VHDX: $($vdisk.FullName)"
24+
25+
# Mount the VHD/VHDX file
26+
Mount-VHD -Path $vdisk.FullName -PassThru | Out-Null
27+
Write-Host "VHD mounted: $($vdisk.Name)"
28+
29+
# Compact the VHD/VHDX file to reclaim unused space
30+
Optimize-VHD -Path $vdisk.FullName -Mode Full
31+
Write-Host "VHD compacted: $($vdisk.Name)"
32+
33+
# Dismount the VHD
34+
Dismount-VHD -Path $vdisk.FullName
35+
Write-Host "VHD dismounted: $($vdisk.Name)"
36+
}
37+
38+
Write-Host "VHD/VHDX compression complete."

docs/WSL-README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Windows.
9090
### To free up space
9191

9292
To free up space you can use the clean.bat/clean.sh scripts under
93-
/src/SDK/Utilities.
93+
/devops\install.
9494

9595
For Windows
9696
```cmd

0 commit comments

Comments
 (0)