从右键单击菜单以管理员身份在特定目录上打开 PowerShell

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

我需要一个命令来在给定路径上打开一个新的提升的 PowerShell window。为什么?我想要一个在给定目录上以管理员身份打开 Powershell 的快捷方式(就像按住 shift 并右键单击任何目录会给你,但以管理员身份。)

我使用 this 在我的右键单击菜单中添加了“在此处以管理员身份打开 powershell window”快捷方式。

目前,我有

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

这将在 C:\WINDOWS\system32 中打开提升的 PowerShell,或者:

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

这将在当前目录

上打开一个新的未提升的 powershell window

我试过了

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

但它没有导航到目录。

注意 - %v 成为您在其中右键单击的目录中的路径。

如何正确组合这些命令以在所需目录中打开提升的 PowerShell?

这是 Windows 注册表设置;从 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""""'''

请注意,棘手的引号 ''""""%V""""'' 可确保即使对于包含空格或其他 特殊 字符的路径也不会出错。

@JosefZ 回答很好,可以在注册表上手动执行操作,

但使用 Easy Context Menu v1.6 我可以更快更高效地创建我想要的上下文菜单。

只需转到列表编辑器并创建您想要的子菜单和项目,就可以创建您想要的确切结果!

您可以非常轻松地编辑内容和 disable/enable 菜单。您也可以仅在以管理员身份按住 shift 或 运行 时显示。

看看多么优雅的结果:

多棒的工具!