From 6db2c158bda607519d89fc6c96f7bb9a99a57045 Mon Sep 17 00:00:00 2001 From: David Date: Sat, 25 Apr 2026 18:09:57 +0200 Subject: [PATCH] Added prompt asking user to run as adminstrator (#556) Co-authored-by: Raphire <9938813+Raphire@users.noreply.github.com> --- Win11Debloat.ps1 | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/Win11Debloat.ps1 b/Win11Debloat.ps1 index 5c3e5b4..bb44166 100644 --- a/Win11Debloat.ps1 +++ b/Win11Debloat.ps1 @@ -1,5 +1,3 @@ -#Requires -RunAsAdministrator - [CmdletBinding(SupportsShouldProcess)] param ( [switch]$CLI, @@ -105,7 +103,42 @@ param ( [switch]$HideDriveLetters ) +# Check if script is running as administrator +$isAdmin = ([Security.Principal.WindowsPrincipal] ` + [Security.Principal.WindowsIdentity]::GetCurrent() +).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) +# If script is not running as administrator ask user if they want to allow it +if (-not $isAdmin) { + Write-Host "Win11Debloat must be run as Administrator." -ForegroundColor Red + + $choice = Read-Host "Restart as Administrator? (y/n)" + + if ($choice -match '^[Yy]$') { + $elevatedArgs = @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", $PSCommandPath) + + foreach ($paramName in $PSBoundParameters.Keys) { + $paramValue = $PSBoundParameters[$paramName] + + if ($paramValue -is [System.Management.Automation.SwitchParameter]) { + if ($paramValue.IsPresent) { + $elevatedArgs += "-$paramName" + } + } + else { + $elevatedArgs += "-$paramName" + $elevatedArgs += "$paramValue" + } + } + + if ($MyInvocation.UnboundArguments.Count -gt 0) { + $elevatedArgs += $MyInvocation.UnboundArguments + } + + Start-Process powershell -ArgumentList $elevatedArgs -Verb RunAs + } + exit +} # Define script-level variables & paths $script:Version = "2026.04.05"