From 0eeb8f323ab9760cc18f8841cc1649b6f7c545ee Mon Sep 17 00:00:00 2001 From: HetCreep Date: Sat, 4 Jul 2026 22:35:24 +0700 Subject: [PATCH] fix(elevation): quote script path and parameter values in admin relaunch (#683) Co-authored-by: Jeffrey <9938813+Raphire@users.noreply.github.com> --- Win11Debloat.ps1 | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Win11Debloat.ps1 b/Win11Debloat.ps1 index 187979b..1917680 100644 --- a/Win11Debloat.ps1 +++ b/Win11Debloat.ps1 @@ -111,7 +111,14 @@ if (-not $isAdmin) { $choice = Read-Host "Restart as Administrator? (y/n)" if ($choice -match '^[Yy]$') { - $elevatedArgs = @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", $PSCommandPath) + # Win32-safe escaping for arguments to pass to elevated process + function Format-ElevatedArg([string]$Value) { + $escaped = $Value -replace '(\\*)"', '$1$1\"' + $escaped = $escaped -replace '(\\+)$', '$1$1' + return '"' + $escaped + '"' + } + + $elevatedArgs = @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", (Format-ElevatedArg $PSCommandPath)) foreach ($paramName in $PSBoundParameters.Keys) { $paramValue = $PSBoundParameters[$paramName] @@ -123,12 +130,14 @@ if (-not $isAdmin) { } else { $elevatedArgs += "-$paramName" - $elevatedArgs += "$paramValue" + $elevatedArgs += (Format-ElevatedArg $paramValue) } } if ($MyInvocation.UnboundArguments.Count -gt 0) { - $elevatedArgs += $MyInvocation.UnboundArguments + foreach ($unboundArg in $MyInvocation.UnboundArguments) { + $elevatedArgs += (Format-ElevatedArg "$unboundArg") + } } Start-Process powershell -ArgumentList $elevatedArgs -Verb RunAs