Last active
November 28, 2024 06:38
-
-
Save Jonesckevin/9c4842de9ea745de1fdb1f04d6aa89be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Check if running with administrative privileges | |
| $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") | |
| if (-not $isAdmin) { | |
| $psi = New-Object System.Diagnostics.ProcessStartInfo | |
| $psi.FileName = 'powershell' | |
| $psi.Arguments = "-Command Start-Process -Verb RunAs -FilePath $PSCommandPath" | |
| $psi.Verb = 'runas' | |
| [System.Diagnostics.Process]::Start($psi) | Out-Null | |
| Exit | |
| } | |
| # Get all WSL VHDX files | |
| $vhdxFiles = Get-ChildItem -Path "$env:LOCALAPPDATA\Packages\CanonicalGroupLimited.Ubuntu*\LocalState\*.vhdx" | |
| # Compact the VHDX files | |
| foreach ($vhdxFile in $vhdxFiles) { | |
| Write-Host "Compacting $($vhdxFile.Name)..." | |
| Optimize-VHD -Path $vhdxFile.FullName -Mode Full | |
| } | |
| Write-Host "VHDX files compacted successfully." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment