|
6 | 6 | # 2. You have Hyper-V management tools: |
7 | 7 | # Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All -All -NoRestart |
8 | 8 |
|
| 9 | +# Check if the script is running with administrator privileges |
| 10 | +$IsAdmin = [bool]([System.Security.Principal.WindowsIdentity]::GetCurrent().Owner.IsWellKnown([System.Security.Principal.WellKnownSidType]::BuiltinAdministratorsSid)) |
| 11 | + |
| 12 | +if (-not $IsAdmin) { |
| 13 | + # Relaunch the script with administrator privileges |
| 14 | + $argList = "$($myinvocation.MyCommand.Path)" # Get the script path |
| 15 | + Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File $argList" -Verb RunAs |
| 16 | + exit |
| 17 | +} |
9 | 18 |
|
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 | | - |
| 19 | +# Shutdown WSL |
16 | 20 | Write-Host "Shutting down WSL..." |
17 | 21 | wsl --shutdown |
18 | 22 | Start-Sleep -Seconds 5 # Wait a few seconds to ensure WSL is fully shut down |
19 | 23 | Write-Host "WSL shut down successfully." |
20 | 24 |
|
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)" |
| 25 | +# Search for all VHDX files under LOCALAPPDATA\...\Packages\... |
| 26 | +$baseFolderPath = Join-Path -Path $env:LOCALAPPDATA -ChildPath "Packages" |
| 27 | +$vdiskFiles = Get-ChildItem -Path $baseFolderPath -Recurse -Filter "ext4.vhdx" |
28 | 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)" |
| 29 | +# Loop through each VHDX file |
| 30 | +foreach ($vdisk in $vdiskFiles) { |
32 | 31 |
|
33 | | - # Dismount the VHD |
34 | | - Dismount-VHD -Path $vdisk.FullName |
35 | | - Write-Host "VHD dismounted: $($vdisk.Name)" |
| 32 | + $diskName = $vdisk.Name |
| 33 | + if ($vdisk.FullName -like "*Ubuntu*") { |
| 34 | + $diskName = "Ubuntu VHDX" |
| 35 | + } elseif ($vdisk.FullName -like "*Debian*") { |
| 36 | + $diskName = "Debian VHDX" |
| 37 | + } |
| 38 | + |
| 39 | + Write-Host "Found ${diskName} at $($vdisk.FullName)" |
| 40 | + |
| 41 | + # Ask the user if they want to compact this VHD/VHDX |
| 42 | + $userInput = Read-Host "Do you want to compact this disk? (Y/N)" |
| 43 | + |
| 44 | + if ($userInput -match "^[Yy]$") { |
| 45 | + # Compact the VHD/VHDX file to reclaim unused space |
| 46 | + Optimize-VHD -Path $vdisk.FullName -Mode Full |
| 47 | + Write-Host "VHD compacted: $($vdisk.Name)" |
| 48 | + } |
| 49 | + # else |
| 50 | + # { |
| 51 | + # Write-Host "Skipping VHD/VHDX: $($vdisk.Name)" |
| 52 | + } |
36 | 53 | } |
37 | 54 |
|
38 | 55 | Write-Host "VHD/VHDX compression complete." |
0 commit comments