16

I have been looking for a way to open an elevated Powershell prompt from Windows Explorer directly, via the context menu of the folder I want to open the prompt in.
I'm using Windows 10 and all the examples I've seen so far have been for older versions of Windows. I previously had this working on Windows 8.1, but the update to 10 broke it. I even got this working on Windows 10 briefly, but an update broke it again (Dec 2015).

Does anyone know the correct way to add this feature to Windows? Or is it doomed to be overwritten by future updates to Windows?

4 Answers 4

26

This is the only way I know of to currently add this feature to context menus in Windows Explorer:

[Run this script in an elevated powershell prompt]

$menu = 'Open Windows PowerShell Here as Administrator'
$command = "$PSHOME\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'"""

'directory', 'directory\background', 'drive' | ForEach-Object {
    New-Item -Path "Registry::HKEY_CLASSES_ROOT\$_\shell" -Name runas\command -Force |
    Set-ItemProperty -Name '(default)' -Value $command -PassThru |
    Set-ItemProperty -Path {$_.PSParentPath} -Name '(default)' -Value $menu -PassThru |
    Set-ItemProperty -Name HasLUAShield -Value ''
}

This script taken from the following link:

http://www.powershellmagazine.com/2013/06/25/pstip-how-to-start-an-elevated-powershell-from-windows-explorer/

I'm 99% certain that this was the way I did it before the latest Windows patch 'removed' my registry setting (it also removed some other customisations, like numlock boot status, but that is less annoying).

If anyone knows a better approach; i.e. that won't be volatile, then please let me know and I'll accept that answer.

5
  • 1
    Windows 10 certainly is a pain with UAC. Even "disabled" it's a constant headache ._. The only reason I haven't gone back to Windows 7 is because I now have 4 screens. Commented Jan 5, 2016 at 16:44
  • 4
    Remove the -NoProfile switch to get your profile loaded automatically when you launch the prompt.
    – Ian Kemp
    Commented Jul 26, 2016 at 13:05
  • Note that if you want to add a "Run script as admin" context-menu option for the ps1 files themselves, section 2 of this answer shows how: stackoverflow.com/a/57033941/2441655
    – Venryx
    Commented Jul 15, 2019 at 6:31
  • This works great as of Windows 1909. It opens the blue window version of PowerShell. Some other solutions open a small, black background window which doesn't work as well.
    – usr
    Commented Feb 21, 2020 at 13:27
  • @usr FYI the black window is actually cmd prompt, the blue is powershell, they are similar but not the same. more info Commented Aug 5, 2020 at 13:13
2

For people who want to open Powershell in elevated Windows Terminal, here is Astravagrant's modified answer.

[Run this script in an elevated powershell prompt]

$menu = 'Open in Terminal as Admin'
$command = 'wt.exe -p "Windows PowerShell" -d "%V"'

'directory', 'directory\background', 'drive' | ForEach-Object {
    New-Item -Path "Registry::HKEY_CLASSES_ROOT\$_\shell" -Name runas\command -Force |
    Set-ItemProperty -Name '(default)' -Value $command -PassThru |
    Set-ItemProperty -Path {$_.PSParentPath} -Name '(default)' -Value $menu -PassThru |
    Set-ItemProperty -Name HasLUAShield -Value ''
}

I modified $command to open wt.exe (Windows Terminal).

-p "Windows PowerShell" flag allows you to choose WT's profile name you want to use. If you want to open WT with default profile, remove -p "..." flag.

-d "%V" flag refers to current folder location

1

I've been doing it like this. It is part of a little menu I made. Edit it to your liking:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\OAPS.Tools]
"ImpliedSelectionModel"=dword:00000001
"Icon"="imageres.dll,-5373"
"ExplorerCommandHandler"="{BF0AC53F-D51C-419F-92E3-2298E125F004}"
@="Admin Pshell Here"
0

Here is a copy of the reg file I use to add both CMD and POWERSHELL to the BACKGROUND context menu of any folder in Windows 10:

Windows Registry Editor Version 5.00

;Add_Open_CMD_and_Powershell_to_Context_Menu.reg

;Right-Click Background only

;CMD Prompt

[HKEY_CLASSES_ROOT\Directory\Background\shell\01MenuCmd] "MUIVerb"="Command Prompts" "Icon"="cmd.exe" "ExtendedSubCommandsKey"="Directory\Background\ContextMenus\MenuCmd"

[HKEY_CLASSES_ROOT\Directory\Background\shell\01MenuCmd] "MUIVerb"="Command Prompts" "Icon"="cmd.exe" "ExtendedSubCommandsKey"="Directory\Background\ContextMenus\MenuCmd"

[HKEY_CLASSES_ROOT\Directory\Background\ContextMenus\MenuCmd\shell\open] "MUIVerb"="Command Prompt" "Icon"="cmd.exe"

[HKEY_CLASSES_ROOT\Directory\Background\ContextMenus\MenuCmd\shell\open\command] @="cmd.exe /s /k pushd \"%V\""

[HKEY_CLASSES_ROOT\Directory\Background\ContextMenus\MenuCmd\shell\runas] "MUIVerb"="Command Prompt Elevated" "Icon"="cmd.exe" "HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\Background\ContextMenus\MenuCmd\shell\runas\command] @="cmd.exe /s /k pushd \"%V\""

; PowerShell

[HKEY_CLASSES_ROOT\Directory\Background\shell\02MenuPowerShell] "MUIVerb"="PowerShell Prompts" "Icon"="powershell.exe" "ExtendedSubCommandsKey"="Directory\Background\ContextMenus\MenuPowerShell"

[HKEY_CLASSES_ROOT\Directory\Background\shell\02MenuPowerShell] "MUIVerb"="PowerShell Prompts" "Icon"="powershell.exe" "ExtendedSubCommandsKey"="Directory\Background\ContextMenus\MenuPowerShell"

[HKEY_CLASSES_ROOT\Directory\Background\ContextMenus\MenuPowerShell\shell\open] "MUIVerb"="PowerShell" "Icon"="powershell.exe"

[HKEY_CLASSES_ROOT\Directory\Background\ContextMenus\MenuPowerShell\shell\open\command] @="powershell.exe -noexit -command Set-Location '%V'"

[HKEY_CLASSES_ROOT\Directory\Background\ContextMenus\MenuPowerShell\shell\runas] "MUIVerb"="PowerShell Elevated" "Icon"="powershell.exe" "HasLUAShield"=""

[HKEY_CLASSES_ROOT\Directory\Background\ContextMenus\MenuPowerShell\shell\runas\command] @="powershell.exe -noexit -command Set-Location '%V'"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .