ChatGPT解决这个技术问题 Extra ChatGPT

如何从 Windows 资源管理器启动 PowerShell?

有没有办法从 Windows 资源管理器在特定文件夹中启动 PowerShell,例如右键单击文件夹并具有“在此文件夹中打开 PowerShell”之类的选项?

每天第一次运行 MSBuild 时,必须将目录更改为我的项目文件夹,这真的很烦人。

[Alt] [F+S+A] 在当前文件夹中启动管理员 powershell。至少对于 Windows 10。

P
Peter Mortensen

在 Windows 资源管理器中,只需转到顶部的地址栏(键盘快捷键:Alt+DCtrl+L) 并输入 powershellpowershell_ise 并按 Enter。 PowerShell 命令窗口将打开当前目录。


当我在 Win 7 上执行此操作时,Windows 资源管理器会将我带到一个名为 PowerShell 的文件夹!
如果您使用的是 Windows 8 或更高版本,则可以简单地使用内置文件 ->“打开 Windows Powershell”
如果您有设置启动目录的 PowerShell 配置文件,它也不起作用。如果您想在没有配置文件的情况下启动,您可以使用“powershell -noprofile”,但是您会丢失配置文件中的所有设置。总会有取舍。
有什么快捷方式可以以管理员身份打开吗?
您也可以键入 powershell_ise 而不是 powershell,这更好,因为它最终可以调整大小并具有适当的字体。
P
Peter Mortensen

只是作为一个技巧反向添加,在 PowerShell 提示符下,您可以执行以下操作:

ii .

或者

start .

在当前目录中打开 Windows 资源管理器窗口。


@EBGreen,ii 是什么意思?
好吧,在 powershell 提示符下,您可以简单地键入: Get-Command ii 然后您会看到它是 Invoke-Item 的别名。
code . 还会在当前文件夹中打开 Visual Studio Code - 这非常方便。
P
Peter Mortensen

如果您使用的是 Windows 8 或更高版本,则可以简单地使用内置文件 → “打开 Windows PowerShell”。

或 Alt + F 后跟 R。


是的。我也固定了“以管理员身份启动”版本。
@Andrej,你是怎么做到的?
@Pacerier,在相应的菜单项上使用鼠标右键(例如,在“打开 Windows PowerShell”上)。
@Andrej,“固定”是指“添加到快速访问工具栏”选项吗?
@Pacerier,我很确定这就是他的意思。您还可以固定到任务栏。打开它后,右键单击任务栏上的图标并将其固定。
6
6 revs, 3 users 93%

作为上述答案的替代方法,它要求您每次都键入 PowerShell 命令 (powershell.exe),您可以创建一个上下文菜单条目,就像使用“在此处打开命令窗口”上下文菜单一样。

这些命令存在三个注册表项。每个键控制不同 Windows 资源管理器对象的上下文菜单。第一个是你问的那个:

HKCR\Directory\Background\shell - 这是资源管理器窗口本身的上下文菜单(即在未选择任何项目时显示的上下文菜单,例如在窗口的空白区域中单击鼠标右键时)。

HKCR\Directory\shell - 这是 Windows 资源管理器中文件夹的上下文菜单。

HKCR\Drive\shell - 这是 Windows 资源管理器根目录中驱动器图标的上下文菜单。

对于这些注册表项中的每一个,您都可以添加一个子项,该子项会将“在此处打开 PowerShell 窗口”命令添加到上下文菜单中,就像您拥有“在此处打开命令窗口”上下文菜单一样。

这是我的 OpenPowerShellHere.reg 文件的副本,该文件将命令放在每个资源管理器对象、窗口背景、文件夹和驱动器图标的上下文菜单中:

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

因此,使用您最喜欢的文本编辑器,打开一个名为 OpenPowerShellHere.reg 的新文件。复制上面代码的确切文本,将其粘贴到新文件中,然后保存。 (我会包含该文件的副本,但我无法确定附件是否可能。)如果您想从其中一个条目中排除该命令,只需用分号注释掉相应的部分。我的评论向您展示了每个部分。

保存文件后,双击它运行它。当它询问时,告诉它继续。一旦你运行它,上下文菜单条目就会出现!

这是我的资源管理器窗口上下文菜单。我已经突出显示了控制台和 PowerShell 命令。如您所见,您还可以添加上下文菜单项来运行提升的命令窗口,即以管理员身份运行。

https://i.stack.imgur.com/aAR1T.png

注意:上下文菜单条目根据其注册表项按字母顺序显示。提升的命令外壳的键名称是“runas”,这就是它出现在 PowerShell 条目之后的原因。

注意:如果您打开了资源管理器窗口,您可能需要将其关闭并重新打开以使更改生效。

注意:在 Windows 7 中,如果您使用资源管理器一侧的工具包,HKCR\Directory\Shell 将不起作用

(即单击库标题下的文档)

您必须使用 Computer -> C: -> to -> Some -> Target -> Directory 导航


这里的一个问题是,当我使用此上下文菜单打开一个 PS 窗口并输入 git help <cmd> 时,它会将 PS 窗口中的 html 帮助文件作为纯文本转储。当我使用开始菜单时,该 PS 窗口会按预期打开浏览器。
@ProfK,我不确定那里发生了什么,因为我不使用“git”,但是,使用此上下文菜单打开的 PS 窗口与使用“开始”菜单打开的 PS 窗口相同,除非你是从“开始”菜单运行 PowerShell ISE。如果是这种情况,您可以将上述注册表文件中的“powershell.exe”更改为“powershell_ise.exe”。如果您运行的是常规 PowerShell,那么除了它们启动的目录之外,没有任何区别。开始菜单命令在您的用户目录中启动,而上下文菜单在本地目录中启动。
@ProfK,是否有可能是起始目录有所不同?如果您的用户目录中有关于“git”的内容,也许这就是区别。上下文菜单的全部目的是在 LOCAL 目录中打开 PS,但如果“git”命令根据您所在的目录运行不同,则可能是问题所在。目录的区别以及PS和PS ISE的区别是我唯一能想到的。
@geo 感谢所有反馈,但我发现在资源管理器中双击 html 文件会在 Notepad++ 中打开它们。我将默认的“打开方式”设置为 Chrome,但同时我也安装了 Posh-Git,现在我的 Git 帮助在 Chrome 中打开,而不是 PS。可悲的一面是我不知道哪个改变了固定的东西。
添加提升的 PS 提示的 PS 脚本(删除非提升的 HasLUAShield 行)是 @powershellmagazine.com/2013/06/25/…
G
Gulzar Nazim

您可以从此处下载 inf 文件 - Introducing PowerShell Prompt Here


你能提供一个总结吗(例如,链接失效总是一个问题)?
d
default

在 Windows 10 中,非管理员和管理员都可以通过菜单栏找到命令提示符和 powershell 提示符。这些选项会将其文件夹设置为资源管理器中当前选择的文件夹。

至少对于瑞典语版本,powershell 是用 Alt F+I 打开的。对于管理员 powershell,它是 Alt F+S+P。

https://i.stack.imgur.com/Ac8S8.png

如果这些不是正确的字符,您可以按住 Alt 键查看正确的字符。每个步骤的菜单项上都会有一个字符。


我的 Windows 10 上的管理员 powershell 是 [Alt] [F+S+A](英文版)
C
Craigo

Windows 10 让它变得更容易。您可以:

SHIFT + 鼠标右键单击一个文件夹,你会得到一个菜单项 Open PowerShell window here。

或者您可以:

文件 -> 打开 Windows PowerShell。

为了奖金......

如果您在 File鼠标右键单击 -> Open Windows PowerShell,那么您可以Add to Quick Access Toolbar

https://i.stack.imgur.com/G5QAG.png

这在这里放了一个方便的图标:

https://i.stack.imgur.com/YIBBm.png

现在您只需单击该图标即可。 :)


这是正确的答案。
A
Andrew

我很惊讶没有人给出这个答案,这是最简单的一个。 (必须是年份。)

只需 Shift + 右键单击资源管理器。然后您可以“在此处打开 PowerShell 窗口”。

默认情况下,它可能设置为命令提示符。如果是这样,您可以在 Windows 10 设置中更改此设置:转到个性化 -> 任务栏并启用“当我右键单击开始按钮或按 Windows 键+X 时,在菜单中将命令提示符替换为 Windows PowerShell”。


J
James Manning

我想让这个上下文菜单只有在右键单击并按住“SHIFT”时才能工作,这就是内置的“在此处打开命令窗口”上下文菜单的工作方式。

然而,所提供的解决方案都没有这样做,所以我不得不滚动我自己的 .reg 文件 - 复制下面的文件,将其另存为 power-shell-here-on-shift.reg 并双击它。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\powershell]
@="Open PowerShell here"
"NoWorkingDirectory"=""
"Extended"=""

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

https://i.stack.imgur.com/daHsy.png


P
Peter Mortensen

试试 PowerShell PowerToy... 它为 Open PowerShell Here 添加了一个上下文菜单项。

或者,您可以创建一个打开 PowerShell 的快捷方式,其中 Start In 文件夹是您的 Projects 文件夹。


p
ppw0

在较新的版本中,当您在空白区域上 Shift+鼠标右键单击 时,默认情况下,Windows 10 在上下文菜单中具有“在此处打开 PowerShell 窗口”,您应该无论如何现在都在使用 Windows Terminal


W
Warren Rumak

在 Windows 8.1 和 Server 2012 R2 中更容易。

这样做一次:右键单击任务栏,选择属性。在导航选项卡中,当我右键单击左下角或按 Windows 键+X 时,打开菜单中的 [✓] 将命令提示符替换为 Windows PowerShell。

然后,每当您需要 PowerShell 提示符时,请按 Win+X, I。(或 Win+X, A 用于 Admin PowerShell 提示符)


不错的快捷方式,但是这不会在当前文件夹中启动 PowerShell。
P
Peter Mortensen

为 SVN 制作工具的家伙制作了一个 Windows Explorer 扩展,它至少会打开一个命令提示符窗口。

我还没有尝试过,所以我不知道它是否会做 PowerShell,但我想与我的 Stack Overflow 兄弟分享爱:

http://tools.tortoisesvn.net/StExBar


M
Mark Larson

另一种选择是 Michael Murgolo 在 TechNet 在 http://technet.microsoft.com/en-us/magazine/2008.06.elevation.aspx 上的出色 Elevation PowerToys。

它们包括 PowerShell Prompt Here 和 PowerShell Prompt Here as Administrator。


虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。
这是发布链接时要考虑的一个有效点;但是,链接的文章包含 Elevation PowerToys 的权威下载,这是答案的重要部分。
R
Rhys

通过在下面添加注册表项,在 Windows 10 中,我设法在我的 SHIFT + RClick 上下文菜单中获得了 Open PowerShell Here 选项。只需将这些复制到一个空白记事本文件中,然后另存为 .reg 文件并运行该文件以添加密钥,它应该可以从那里工作。其中一些其他答案说要将密钥添加到 HKCR\Directory\shell 但我发现它只适用于进入 HKLM\SOFTWARE\Classes\Directory\shell< /强>

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\powershell]
"Extended"=""
"NoWorkingDirectory"=""
@="Open PowerShell here"
"Icon"="%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"

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

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\powershell]
@="Open PowerShell here"
"Extended"=""
"Icon"="%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"
"NoWorkingDirectory"=""

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

f
frank tan
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
if(-not (Test-Path -Path "HKCR:\Directory\shell\$KeyName"))
{
    Try
    {
        New-Item -itemType String "HKCR:\Directory\shell\$KeyName" -value "Open PowerShell in this Folder" -ErrorAction Stop
        New-Item -itemType String "HKCR:\Directory\shell\$KeyName\command" -value "$env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command Set-Location '%V'" -ErrorAction Stop
        Write-Host "Successfully!"
     }
     Catch
     {
         Write-Error $_.Exception.Message
     }
}
else
{
    Write-Warning "The specified key name already exists. Type another name and try again."
}

您可以从 how to start PowerShell from Windows Explorer 下载详细脚本


W
Walter Mitty

一种相当简单的替代方法是通过快捷方式调用 PowerShell。有一个标记为“开始于”的快捷方式属性,说明调用快捷方式时要使用的目录(文件夹)。

如果 Start In 框为空,则表示使用当前目录。

当您第一次以通常的方式创建 PowerShell 的快捷方式时,开始在框中指定主目录。如果你把 start in 框空白,你现在有一个 powershell 的快捷方式,可以在当前目录中打开 PS,不管是什么。

如果您现在将此快捷方式复制到目标目录,并使用资源管理器调用它,您将启动一个指向目标目录的 PS。

这个问题已经有一个公认的答案,但我将其作为另一种方式提供。


n
np8

对于自动热键用户,这是我正在使用的一个片段

Ctrl-Alt-T 时,它会打开 PowerShell 窗口。 (用Win10测试)

如果您的“活动窗口”是 Windows 资源管理器窗口,则 PowerShell 在当前文件夹中打开。否则,只需在某个默认文件夹中打开 PowerShell。

用法: 1) 安装 AutoHotkey,并将其复制粘贴到 myscript.ahk 2) 将 <DefaultPath> 替换为您选择的路径。 3) 运行脚本。

; Ctrl-Alt-T opens PowerShell in the current folder, if using Windows Explorer. Otherwise, just open the Powershell.
^!T::
if WinActive("ahk_class CabinetWClass") and WinActive("ahk_exe explorer.exe")
{
    KeyWait Control
    KeyWait Alt
    Send {Ctrl down}l{Ctrl up}
    Send powershell{Enter}
}
else
{
    psScript =
    (
    cd 'C:\<DefaultPath>'
    ls
    )
    Run "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" -NoExit -Command &{%psScript%}
}
return

B
Beliaev Maksim

https://i.stack.imgur.com/7fO0w.jpg

这会将注册表项的所有者更改为管理员并添加上下文菜单
Change registry to enable PS and CWD context menus


T
Tim Truston

只有这在 Windows 10 上对我有用......

使用以下内容创建一个名为 PowershellHereContextMenu.reg 的文件,右键单击它并“合并”。

Windows Registry Editor Version 5.00

;
; Add context menu entry to Windows Explorer folders
;
[HKEY_CLASSES_ROOT\Directory\shell\powershellmenu]
@="PowerShell Here"

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

;
; Add context menu entry to Windows Explorer background
;
[HKEY_CLASSES_ROOT\Directory\Background\shell\powershellmenu]
@="PowerShell Here"
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\powershellmenu\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\powershellmenu]
@="PowerShell Here"
"NoWorkingDirectory"=""

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


J
Jai advith

在文件资源管理器的任何位置以管理员身份运行 PowerShell 转到该文件夹并使用快捷方式“alt + f + s + a”在该特定文件夹位置以管理员身份打开 powershell


s
sudhAnsu63

您可以在 Windows 资源管理器地址栏上运行以下命令打开 powershell ,它将打开该目录的路径。

powershell.exe -noexit -command "写主机 "Hello World""