Skip to content

Instantly share code, notes, and snippets.

@Jonesckevin
Last active November 28, 2024 06:38
Show Gist options
  • Select an option

  • Save Jonesckevin/9c4842de9ea745de1fdb1f04d6aa89be to your computer and use it in GitHub Desktop.

Select an option

Save Jonesckevin/9c4842de9ea745de1fdb1f04d6aa89be to your computer and use it in GitHub Desktop.
# 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