Right-click Powershell as Admin – not quite as hacky as you thought it would be

For centuries, man has longed for a way to start the Powershell with administrator privileges in their current folder. Ages ago it was solved by first launching the shell, and have it launch itself again with administrator privileges, causing a flicker as the old window dissappeared. Yesterday I suceeded in launching it, with administrative rights, without the flicker!

TL;DR: The solution was to make a copy of the program, and always run this as an administrator the original doesn’t have this option.

Making an admin-version of Powershell to run.

Go to your favourite `system32\WindowsPowerShell\v1.0` folder and make a copy of the program `powershell.exe`. Call it something cool like `powersheaven.exe` (beacuse it will be).
1. `Right click` on it
2. Choose `compatibility` and
3. Check `run as administrator`
4. Click `Apply` and `Ok`. (Cool kids just click `Ok`.)

NB: if you rename it after you set the compatibility settings, you’ll have to set them again.

Making a right-click shortcut

(You’ll be changing the registry here, so pay attention, here be dragons.)

1. Open `regedit` and navigate to the key `Computer\HKEY_CLASSES_ROOT\Directory\Background\shell`
2. Add a new key, call it something rememberable, like `Powersheaven`
3. The `(standard)` string will be the name of command, call it something cool, like `Powersheaven`.

It should look something like this now:

Next,
1. Add a sub-key to `Powersheaven` called `command`
2. The `(standard)` string should have the value of the program you’re trying to launch. This is `powersheaven.exe`, but with the usual arguments for keeping it open and current directory. Mine became `C:\Windows\System32\WindowsPowerShell\v1.0\powersheaven.exe -noexit -command Set-Location -literalPath ‘%V’` which was shamelessly stolen from the `command`-key of the real Powershell. With the program changed of course. You need the absolute path for out-of-scope reasons.

It should look something like this now:

Making the right-click shortcut cool, in order of usefulness

– (Add and change the values under the main Powersheaven key)

A shortcut key for your command.

– Add an `&` (ampersand) before a letter in the name, I changed the name to `Po&wersheaven`, so I can open it by pressing `w`.

A cool icon.

– Add a new string called `icon`, and give it the value of a program, this will be the small icon you see, so `C:\Windows\System32\WindowsPowerShell\v1.0\powersheaven.exe` is probably a good one.

Only display when `shift` is pressed, like the original.

– Add a string called `Extended`, leave the value empty.

Add a tiny UAC-shield to the icon, to make it look super Microsoft and adminish.

– Add a string called `HasLUAShield`, leave the value empty.

Try it out in a folder, and see that it works, if you can run the command `[bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match “S-1-5-32-544”)` and it returns `true`, then congratulations and enjoy Powersheaven!

In my world, it ended up looking like this:

And ran like this:

NB: always remember to lock your workstation, or you might end up with a new wallpaper.

Mad credits to:

The blogpost Add Open Powershell here to context menu by Ashwin Sinha which helped immensely with the initial regedit.

The Detect if powershell is running as administrator solution

The `.reg` scripts from this tutorial where I derived the keys for `shift` and the `UAC shield`.

2 Comments

Leave a Reply to Some dude Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.