brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Start-Process -Verb RunAs opens an elevated program from PowerShell

Security
~/tips/tip-windows-start-process-runas

Start-Process -FilePath "powershell" -Verb RunAs

Starts one program with administrator rights from a normal PowerShell window.

I would rather not run a whole terminal as administrator all day. Starting just the one program that needs it from a normal window is tidier.

The docs for Start-Process have an example that starts PowerShell with the Run as administrator option:

Start-Process -FilePath "powershell" -Verb RunAs

A new elevated window opens, and your current session stays a normal one. The -Verb parameter picks the action Windows takes with the file, and the available verbs depend on the file name extension. The docs list Open, RunAs and RunAsUser for .exe files, and add Edit and Print for .cmd files. To see the verbs for any file, the docs create a ProcessStartInfo object and read its Verbs property:

$p = New-Object System.Diagnostics.ProcessStartInfo -Args powershell.exe
$p.Verbs

You can pass arguments with -ArgumentList and wait for the result with -Wait. For a single command that runs elevated and then closes:

Start-Process -FilePath "$Env:ComSpec" -ArgumentList "/c ipconfig /flushdns" -Verb RunAs -Wait

That is a Command Prompt that runs the command after /c, with elevation, while PowerShell waits for it to finish. If an argument holds spaces, the docs say it needs escaped double quotes inside the string, and for best results they recommend one -ArgumentList value that contains all arguments.

The caveats come from the parameter sets. Looking at the syntax in the docs, -Verb belongs to a different parameter set than -Credential, -NoNewWindow and the -Redirect* parameters, so you cannot combine them. That means no capturing of the output of the elevated process into a file with -RedirectStandardOutput. The docs also say -Verb does not apply on non-Windows systems.

And a general one: an elevated window can change the whole system, so only start something you trust. The docs flag -FilePath as a security risk when it comes from untrusted data.

Further reading: Start-Process

More Security tips tcpdump -nn -r capture.pcap port 53 mkcert -install Strict-Transport-Security: max-age=0 Referrer-Policy: strict-origin-when-cross-origin wp super-admin list All Security tips (79) All topics

Link