0

There is a great community wiki comment that demonstrates adding a context menu entry (though, without admin rights). How would you change the registry keys or the following .reg file to open PowerShell with Administrator privileges?

Windows Registry Editor Version 5.00

;
; Add context menu entry to Windows Explorer background
;
[HKEY_CLASSES_ROOT\Directory\Background\shell\powershell]
@="Open PowerShell window here"
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\powershell\command]
@="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"

;
; Add context menu entry to Windows Explorer folders
;
[HKEY_CLASSES_ROOT\Directory\shell\powershell]
@="Open PowerShell window here"
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Directory\shell\powershell\command]
@="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"

;
; Add context menu entry to Windows Explorer drive icons
;
[HKEY_CLASSES_ROOT\Drive\shell\powershell]
@="Open PowerShell window here"
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Drive\shell\powershell\command]
@="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"
Community
  • 1
  • 1
spflow
  • 1,010
  • 7
  • 12
  • 1
    possible duplicate of [How to set "Run this program as an administrator" programatically](http://stackoverflow.com/questions/2313045/how-to-set-run-this-program-as-an-administrator-programatically) – Nathan Rice Mar 05 '15 at 00:43
  • thanks for the response, but I wasn't able to gather much from any of those answers. – spflow Mar 05 '15 at 23:36
  • REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" /t REG_SZ /d "RUNASADMIN" /f – Nathan Rice Mar 05 '15 at 23:44
  • ^ this would be the command you'd run if you wish to add an entry to the registry. Alternatively, you could add that to your reg file. – Nathan Rice Mar 05 '15 at 23:45
  • [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers] "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"="RUNASADMIN" – Nathan Rice Mar 05 '15 at 23:46
  • That's funny. I was just posting a workaround similar to this. Not a perfect answer but works for me. Thanks Nathan. – spflow Mar 05 '15 at 23:59

2 Answers2

0

Workaround:

Force PowerShell to open as admin every time by adding an entry to registry key located at:

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers\

Key name = Path - example:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Value Data:

RUNASADMIN



This isn't exactly how I wanted to do it but it works for my purposes.

Community
  • 1
  • 1
spflow
  • 1,010
  • 7
  • 12
0

Using the key runas would cause whatever command to run as administrator. Below you can see part of the registry tweak that I use, or you can see the full .reg file I use on my GitHub Gist

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\runas]
@="Open elevated PowerShell here"
"Extended"=""
"Icon"="powershell.exe"
"NoWorkingDirectory"=""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\runas\command]
@="C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\runas]
"Extended"=""
"NoWorkingDirectory"=""
@="Open elevated PowerShell here"
"Icon"="powershell.exe"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\runas\command]
@="C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"
Rhys
  • 132
  • 8