给Windows右键菜单添加powershell.exe执行菜单
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\shell\Open_PowerShell] @="Open PowerShell here" "Icon"="powershell.exe" [HKEY_CLASSES_ROOT\Directory\shell\Open_PowerShell\command] @="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'" [HKEY_CLASSES_ROOT\Directory\Background\shell\Open_PowerShell] @="Open PowerShell here" "Icon"="powershell.exe" [HKEY_CLASSES_ROOT\Directory\Background\shell\Open_PowerShell\command] @="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'" [HKEY_CLASSES_ROOT\Drive\shell\Open_PowerShell] @="Open PowerShell here" "Icon"="powershell.exe" [HKEY_CLASSES_ROOT\Drive\shell\Open_PowerShell\command] @="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"
这段代码是一个Windows注册表脚本,用于在文件资源管理器的右键菜单中添加"Open PowerShell here"选项,以便快速在当前位置打开PowerShell终端。以下是对代码的详细解释:
**注册表脚本结构**
```
Windows Registry Editor Version 5.00 # 声明注册表文件版本
[注册表路径] # 创建或修改指定路径下的注册表项
@="默认值" # 设置该项的默认值(显示的菜单项文本)
"键名"="键值" # 设置命名值(如图标、命令等)
```
**关键部分解析**
1. 为目录(文件夹)添加右键菜单**
```
[HKEY_CLASSES_ROOT\Directory\shell\Open_PowerShell]
@="Open PowerShell here"
"Icon"="powershell.exe"
```
- `Directory\shell`:针对文件夹本身右键时生效。
- `Open_PowerShell`:菜单项的内部名称。
- `@="Open PowerShell here"`:显示在右键菜单中的文本。
- `Icon=powershell.exe`:使用PowerShell的图标。
2. 配置目录右键菜单的命令**
```
[HKEY_CLASSES_ROOT\Directory\shell\Open_PowerShell\command]
@="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%V'"
```
- `command`子项:定义菜单项点击后执行的命令。
- `powershell.exe`:PowerShell可执行文件的完整路径。
- `-NoExit`:执行命令后不关闭PowerShell窗口。
- `-Command Set-Location -LiteralPath '%V'`:切换到当前右键点击的目录(`%V`代表路径,使用`-LiteralPath`避免特殊字符转义问题)。
3. 为目录背景(文件夹空白处)添加右键菜单**
```
[HKEY_CLASSES_ROOT\Directory\Background\shell\Open_PowerShell]
@="Open PowerShell here"
"Icon"="powershell.exe"
```
- `Directory\Background\shell`:针对文件夹空白处右键时生效。
4. 为驱动器(如C盘、D盘)添加右键菜单**
```
[HKEY_CLASSES_ROOT\Drive\shell\Open_PowerShell]
@="Open PowerShell here"
"Icon"="powershell.exe"
```
- `Drive\shell`:针对磁盘驱动器右键时生效。
**实际效果**
安装此注册表脚本后,在文件资源管理器中:
1. **右键点击文件夹** → 显示"Open PowerShell here"选项。
2. **右键点击文件夹空白处** → 显示"Open PowerShell here"选项。
3. **右键点击驱动器(如C盘)** → 显示"Open PowerShell here"选项。
点击该选项后,会打开PowerShell终端,且当前路径自动设置为右键操作的位置(文件夹、驱动器或空白处)。
**技术细节**
1. **路径转义**:
- 注册表中使用双反斜杠`\\`表示单个反斜杠(如`C:\\Windows`)。
2. **参数说明**:
- `-NoExit`:保持PowerShell窗口打开,方便继续执行命令。
- `-Command Set-Location -LiteralPath '%V'`:
- `Set-Location`:等同于`cd`命令,用于切换目录。
- `-LiteralPath`:逐字解释路径,避免路径中包含`[]`等特殊字符时出错。
- `%V`:注册表变量,表示右键操作的路径(类似批处理中的`%1`)。
3. **兼容性**:
- 该脚本适用于Windows 7及以上系统(PowerShell 2.0+)。
- 对于Windows 10/11用户,系统默认已添加"Open PowerShell window here"选项,但此脚本可自定义菜单项名称和图标。
**对比与扩展**
- **与CMD脚本的区别**:
- 此脚本使用PowerShell(功能更强大),而CMD脚本使用`cmd.exe`。
- PowerShell支持更多高级命令和语法(如管道、对象操作)。
- **扩展建议**:
- 若需添加"Open PowerShell as Administrator"选项,可复制类似结构并修改命令为:
```
powershell.exe -NoExit -Command Start-Process powershell.exe "-NoExit -Command Set-Location -LiteralPath '%V'" -Verb RunAs
```
通过此脚本,用户可以更高效地在指定位置打开PowerShell终端,提升文件管理和命令行操作的便捷性。