Windows UAC Elevation Skill
Guidelines and syntax patterns for executing administrative/elevated commands under Windows UAC from a standard user session without silent failure.
Windows UAC Elevation Skill
When executing commands on Windows that require administrative privileges (e.g., registry modifications under HKLM, service controls, system-wide process termination, or security configuration changes) from a standard/non-elevated runner, follow these rules to prevent silent execution failure.
The Core Problem
- Detached UAC Session: Any process started with
-Verb RunAsruns in a separate security context. The parent standard-user runner (IDE) cannot capture its stdout/stderr. Spawning the wrapper is successful (ExitCode: 0), but the inner action may have crashed. - Double-Parsing of Quotes: Spawning
powershell.exe -Command "<admin command>"elevations mangles nested quotes, leading to silent syntax crashes inside the elevated window (which closes instantly).
Required Execution Protocol
Rule 1: Prefer Elevated CMD over PowerShell
For standard system tasks, always wrap execution in cmd.exe instead of powershell.exe. Use the native system tools directly:
- Registry Writes: Use
reg.exeinsidecmd.exe /c - Service Management: Use
net.exeorsc.exeinsidecmd.exe /c - Process Killing: Use
taskkill.exeinsidecmd.exe /c
Rule 2: Concrete Syntax Patterns
A. Registry Modifications
Start-Process cmd.exe -ArgumentList "/c reg add `"HKLM\SOFTWARE\Path\To\Key`" /v `"ValueName`" /t REG_SZ /d `"ValueData`" /f" -Verb RunAs -Wait
Start-Process cmd.exe -ArgumentList "/c reg delete `"HKLM\SOFTWARE\Path\To\Key`" /f" -Verb RunAs -Wait
B. Service Control
Start-Process cmd.exe -ArgumentList "/c net stop `"ServiceName`" & sc config `"ServiceName`" start= disabled" -Verb RunAs -Wait
C. Force Process Kill
Start-Process cmd.exe -ArgumentList "/c taskkill /F /IM `"ProcessName.exe`"" -Verb RunAs -Wait
Rule 3: Post-Execution Verification
Because UAC prompts always return ExitCode: 0 to the non-elevated IDE, you must never assume the command succeeded.
- Immediately execute a follow-up, read-only query (e.g.,
reg query,Get-Service, orGet-Process) in your standard user shell to verify that the state change has actually been applied.