Windows 添加右键以管理员身份运行 PowerShell

在 Windows 系统中添加一个右键菜单选项,以便可以使用管理员权限打开 PowerShell,可以通过编辑注册表来实现。

  1. 打开注册表编辑器

    • Win + R 打开运行对话框。
    • 输入 regedit 并按回车,这将打开注册表编辑器。
  2. 导航到文件夹背景键

    • 在注册表编辑器中,导航到以下路径:

      复制代码
      HKEY_CLASSES_ROOT\Directory\Background\shell
  3. 创建新的键

    • 右键点击 shell,选择 新建 -> ,命名为 Open PowerShell Here as Administrator
  4. 设置命令

    • 在你刚刚创建的 Open PowerShell Here as Administrator 键上右键点击,选择 新建 -> ,命名为 command
    • command 键的默认值中输入命令。

这里已经将这个操作封装为了一个注册表编辑文件:

1、添加右键 Open PowerShell as Admin

bash 复制代码
Windows Registry Editor Version 5.00

; Created by:Shawn Brink
; Created on: August 13, 2016
; Updated on: December 20, 2023
; Tutorial: https://www.tenforums.com/tutorials/60177-add-open-powershell-window-here-administrator-windows-10-a.html

[HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin]
@="Open PowerShell window here as administrator"
"Extended"=-
"HasLUAShield"=""
"Icon"="powershell.exe"

[HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin\command]
@="cmd /c reg add hkcu\\software\\_dir /d \"%v\" /f & start powershell -WindowS H -noP -c Start-Process -v RunAs powershell.exe '-noL -noE -c cd -literalP (gp hkcu:\\software\\_dir).\\\"\\\"\\\"(default)\\\"\\\"\\\"; ri hkcu:\\software\\_dir'"


[HKEY_CLASSES_ROOT\Directory\shell\PowerShellAsAdmin]
@="Open PowerShell window here as administrator"
"Extended"=-
"HasLUAShield"=""
"Icon"="powershell.exe"

[HKEY_CLASSES_ROOT\Directory\shell\PowerShellAsAdmin\command]
@="cmd /c reg add hkcu\\software\\_dir /d \"%v\" /f & start powershell -WindowS H -noP -c Start-Process -v RunAs powershell.exe '-noL -noE -c cd -literalP (gp hkcu:\\software\\_dir).\\\"\\\"\\\"(default)\\\"\\\"\\\"; ri hkcu:\\software\\_dir'"


[HKEY_CLASSES_ROOT\Drive\shell\PowerShellAsAdmin]
@="Open PowerShell window here as administrator"
"Extended"=-
"HasLUAShield"=""
"Icon"="powershell.exe"

[HKEY_CLASSES_ROOT\Drive\shell\PowerShellAsAdmin\command]
@="cmd /c reg add hkcu\\software\\_dir /d \"%v\" /f & start powershell -WindowS H -noP -c Start-Process -v RunAs powershell.exe '-noL -noE -c cd -literalP (gp hkcu:\\software\\_dir).\\\"\\\"\\\"(default)\\\"\\\"\\\"; ri hkcu:\\software\\_dir'"


[-HKEY_CLASSES_ROOT\LibraryFolder\Background\shell\PowerShellAsAdmin]


; To allow mapped drives to be available in elevated PowerShell
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
"EnableLinkedConnections"=dword:00000001

2、删除右键中的PowerShell调用命令

bash 复制代码
Windows Registry Editor Version 5.00

[-HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShellAsAdmin]

[-HKEY_CLASSES_ROOT\Directory\shell\PowerShellAsAdmin]

[-HKEY_CLASSES_ROOT\Drive\shell\PowerShellAsAdmin]

[-HKEY_CLASSES_ROOT\LibraryFolder\Background\shell\PowerShellAsAdmin]

将上述代码放到一个以.reg结尾的注册表编辑文件运行即可

相关推荐
Abigail_chow3 小时前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
love530love5 小时前
【笔记】在 MSYS2(MINGW64)中正确安装 Rust
运维·开发语言·人工智能·windows·笔记·python·rust
代码搬运媛6 小时前
“packageManager“: “[email protected]“ 配置如何正确启动项目?
windows·webpack
小道士写程序7 小时前
Qt 5.12 上读取 .xlsx 文件(Windows 平台)
开发语言·windows·qt
异常君12 小时前
Windows 与 Linux 虚拟内存机制对比:设计理念与实现差异
java·linux·windows
搏博14 小时前
将图形可视化工具的 Python 脚本打包为 Windows 应用程序
开发语言·windows·python·matplotlib·数据可视化
电手15 小时前
Win10停更,Win11不好用?现在Mac电脑比Win11电脑更便宜
windows·macos·电脑·mac
拾回程序猿的圈圈∞15 小时前
PyCharm项目和文件运行时使用conda环境的教程
windows·pycharm·conda
波点兔16 小时前
【亲测有效 | Cursor Pro每月500次快速请求扩5倍】(Windows版)Cursor中集成interactive-feedback-mcp
windows·mcp·cursor pro
饮长安千年月17 小时前
JavaSec-SSTI - 模板引擎注入
java·windows·安全·web安全·网络安全·系统安全·安全架构