简体   繁体   中英

Run PowerShell, admin mode, in directory from shell context menu

I have created an Explorer Shell cascading context menu for opening PowerShell within a directory using the code shown below. (Note: I have a sample path hard-coded in the admin command for testing.)

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\powershell.exe]
"MUIVerb"=""
"SubCommands"="powershell;powershell_admin"
"Icon"="PowerShell.exe"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\powershell]
@="Open PowerShell here"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\powershell\command]
@="powershell.exe -NoLogo -NoExit -Command Set-Location '%V'"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\powershell_admin]
@="Open PowerShell (admin) here"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\powershell_admin\command]
@="powershell.exe -Command 'Start-Process PowerShell -ArgumentList \"-NoLogo -NoExit -Command Set-Location C:\Python27\" -Verb RunAs'"

The non-admin command works perfectly. The admin command ignores everything in the ArgumentList.

If I open a PowerShell and execute the code within the single-quotes(') directly it works fine. EG:

Start-Process PowerShell -ArgumentList \"-NoLogo -NoExit -Command Set-Location C:\Python27\" -Verb RunAs

But when executing from the context menu it opens in Admin mode but displays the logo and doesn't execute the Set-Location.

Thanks in advance

This is a cool utility. I installed the Registry entries to experiment, and now that it's working I'll probably keep it.

In my experiments the window closed immediately, even before it brought up the UAC dialog, until I enclosed the outer command in escaped double quotes and the inner parameters in single quotes. Then it worked fine for every folder except those under Program Files. For that we need to enclose %v in single quotes, and double-escape the double-quotes surrounding it:

@="powershell.exe -command \"start-process powershell.exe -ArgumentList \\\"-NoLogo -NoExit -Command Set-Location '%v'\\\"  -verb RunAs\""

I used %v - the context folder name, which seemed to be your original intention based on the menu label 'Open Powershell (admin) here'.

Debugging notes:

  • For experimentation it's a little easier to make the changes in Regedit.exe and let the export functionality add the escaping characters as necessary
  • In Windows 10 you can make changes directly within Regedit.exe and the context menu/action is updated immediately, which I confirmed by adding a timestamp to the menu label
  • In other versions of Windows it may be necessary to stop and restart explorer.exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM