Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Open Powershell as Admin on Right-Click (Context Menu) #gistblog #windows #powershell

Open Powershell as Admin on Right-Click

I described how to set a context menu option to open a Powershell console on right click earlier.

To open Powershell as admin on right click, we can add a command to be executed on start to our registry entry like so

$stpath = pwd; Start-Process PowerShell -ArgumentList \"-NoExit\", \"-Command cd $stpath\" -verb RunAs

So our Command in the registry entry will look like the following

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -windowstyle hidden -Command $stpath = pwd; Start-Process PowerShell -ArgumentList \"-NoExit\", \"-Command cd $stpath\" -verb RunAs

The downside of this approach is that first a normal Powershell window is opened, which opens a new Powershell window as admin if the user has the priviledges, and then the original Powershell window closes in the background.

This results in a flicker when Powershell is run as admin. 😢

Run Powershell as Admin on right click

If you find a flickerless approach to doing this, please leave a comment on this gist. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment