open PowerShell as administrator on specific directory from right-click menu

Viewed 755

I need a single command that opens a new elevated PowerShell window on a given path. why? I want a shortcut for opening Powershell as admin on given directory(just like holding shift and right-click on any directory will give you, but as admin.)

I used this to add an "open powershell window as admin here" shortcut to my right-click menu.

currently, I have

powershell -Command "Start-Process PowerShell -Verb RunAs"

that will open elevated PowerShell in C:\WINDOWS\system32 , or:

powershell.exe -noexit -command Set-Location -literalPath '%V'

that will open a new not elevated powershell window on current directory

I tried

powershell -Command "Start-Process PowerShell -Verb RunAs; Set-Location -literalPath '%V'"

but it does not navigate to the directory.

note - %v becomes the path inside the directory you right-clicked in it.

How can I combine correctly these commands to open elevated PowerShell in the wanted directory?

2 Answers

Here's Windows registry setting; works on rigt-click from Windows Explorer:

reg query HKEY_CLASSES_ROOT\Directory\shell\runasPowerShell -s

HKEY_CLASSES_ROOT\Directory\shell\runasPowerShell
    HasLUAShield    REG_SZ
    (Default)    REG_SZ    PowerShell here as administrator

HKEY_CLASSES_ROOT\Directory\shell\runasPowerShell\command
    (Default)    REG_SZ    Powershell Start-Process PowerShell -verb runas -ArgumentList '-noexit', 'Push-Location -literalPath ''""""%V""""'''

Note that the tricky quoting ''""""%V""""'' guarantees mistake-free operation even for paths with spaces or other special characters.

@JosefZ answer is great, and doing things manually on the registry is possible,

but using Easy Context Menu v1.6 I could create my wanted context menu much faster and efficiently.

Just go to the List editor and create the sub-menus and items you wish and could create the exact results you wish!

enter image description here

you can edit things very easily and disable/enable menus whenever you want. you can also show only when holding shift or run as admin.

enter image description here

see what an elegant result:

enter image description here

what a wonderful tool!

Related