在文件夹上下文菜单中添加“以管理员身份在此打开 Powershell”选项

在文件夹上下文菜单中添加“以管理员身份在此打开 Powershell”选项

我一直在寻找一种方法,通过我想要在其中打开提示符的文件夹的上下文菜单,直接从 Windows 资源管理器打开提升的 Powershell 提示符。
我使用的是 Windows 10,到目前为止我看到的所有示例都是针对旧版本的 Windows。我以前在 Windows 8.1 上运行过这个,但更新到 10 后就不行了。我甚至在 Windows 10 上也运行过这个,但更新又把它搞坏了(2015 年 12 月)。

有谁知道正确的为 Windows 添加此功能的方法是什么?还是说它注定会被 Windows 未来的更新所覆盖?

答案1

这是我所知道的当前将此功能添加到 Windows 资源管理器中的上下文菜单的唯一方法:

[在提升的 powershell 提示符下运行此脚本]

$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 ''
}

该脚本取自以下链接:

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

我 99% 确定这是我在最新的 Windows 补丁“删除”我的注册表设置之前所做的方式(它还删除了一些其他自定义设置,例如 numlock 启动状态,但这不那么烦人)。

如果有人知道更好的方法;即不会不稳定,那么请告诉我,我会接受这个答案。

答案2

对于想要Powershell在高架上开门的人Windows Terminal,这是 Astravagrant 的修改版回答

[在提升的 powershell 提示符下运行此脚本]

$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 ''
}

我修改$command为打开wt.exe(Win​​dows 终端)。

-p "Windows PowerShell"标记允许您选择要使用的 WT 配置文件名称。如果您想使用默认配置文件打开 WT,请删除-p "..."标记。

-d "%V"标志指的是当前文件夹位置

答案3

我一直都是这样做的。这是我制作的小菜单的一部分。根据你的喜好进行编辑:

[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"

答案4

这是我用来将 CMD 和 POWERSHELL 添加到 Windows 10 中任何文件夹的 BACKGROUND 上下文菜单的 reg 文件的副本:

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'"

相关内容