有没有办法从Windows资源管理器启动PowerShell在一个特定的文件夹,例如右键单击一个文件夹,并有一个选项,如“打开PowerShell在这个文件夹”?
每天我第一次运行MSBuild时,都要更改项目文件夹的目录,这真的很烦人。
有没有办法从Windows资源管理器启动PowerShell在一个特定的文件夹,例如右键单击一个文件夹,并有一个选项,如“打开PowerShell在这个文件夹”?
每天我第一次运行MSBuild时,都要更改项目文件夹的目录,这真的很烦人。
当前回答
在最新版本的Windows 10中,当你用鼠标Shift+右键点击空白区域时,默认在上下文菜单中有“在此打开PowerShell窗口”,现在你应该已经在使用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进行导航
通过添加下面的注册表键,在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'"
要以管理员身份在文件资源管理器的任何位置运行PowerShell,请到该文件夹,并使用快捷方式“alt + f + s + a”以管理员身份在该特定文件夹位置打开PowerShell
对于自动热键用户,这里有一个片段我正在使用
当按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
您可以在Windows资源管理器地址栏上执行以下命令打开powershell,它将打开该目录的路径。
powershell.exe -noexit -command "Write-Host "Hello World"