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

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


当前回答

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

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

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

使用方法:1)安装AutoHotkey,复制粘贴到myscript。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

其他回答

您可以从这里下载inf文件-介绍PowerShell提示在这里

只是添加一个反向的技巧,在PowerShell提示符下你可以这样做:

ii .

or

start .

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

上面的答案需要您每次都键入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命令。如您所见,您还可以添加一个上下文菜单条目来运行提升的命令窗口,即以管理员身份运行。

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

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

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

(即。点击Libraries标题下的Documents)

您必须使用Computer -> C: ->到-> Some -> Target -> Directory进行导航

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."
}

您可以从Windows资源管理器中下载如何启动PowerShell的详细脚本

一个相当简单的替代方法是通过快捷方式调用PowerShell。有一个标记为“Start in”的快捷方式属性,它表示在调用快捷方式时使用哪个目录(文件夹)。

如果“起始位置”框为空,则表示使用当前目录。

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

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

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