在电脑使用过程中,CMD(命令提示符)是许多新手和专业人士都可能会用到的工具。然而,当你尝试使用CMD执行一些需要管理员权限的操作时,可能会遇到频繁授权的烦恼。其实,设置CMD永久管理员权限并不复杂,下面我将为你详细讲解如何轻松完成这一设置。
1. 了解管理员权限
在Windows操作系统中,管理员权限是最高权限,具有对系统进行修改和管理的权限。当你需要执行一些敏感操作时,系统会要求你输入管理员密码进行验证。
2. 设置CMD永久管理员权限的方法
方法一:通过组策略编辑器
- 按下
Win + R快捷键,输入gpedit.msc并按下回车键,打开组策略编辑器。 - 在左侧导航栏中,依次展开“计算机配置”->“管理模板”->“系统”->“脚本”->“启动”。
- 在右侧窗口中,找到“运行”项,双击打开。
- 在“程序”一栏中,输入以下命令:
powershell -Command "(New-Object System.Security.Principal.NTAccount('Administrators')).Value | (New-Object System.Security.Principal.SecurityIdentifier).Translate(3) | Add-Type -TypeDefinition '[DllImport(\"kernel32.dll\", CharSet = CharSet.Auto)] public static extern bool SetTokenInformation(IntPtr TokenHandle, int TokenInformationClass, byte[] TokenInformation, int TokenInformationLength); [DllImport(\"kernel32.dll\", CharSet = CharSet.Auto)] public static extern bool OpenProcess(int DesiredAccess, bool InheritHandle, int ProcessId); [DllImport(\"kernel32.dll\", CharSet = CharSet.Auto)] public static extern bool CloseHandle(IntPtr Handle); public static void SetElevated() { IntPtr tokenHandle = IntPtr.Zero; try { int processId = [System.Diagnostics.Process]::GetCurrentProcess().Id; IntPtr processHandle = OpenProcess(0x02000000, false, processId); if (processHandle != IntPtr.Zero) { int tokenResult = GetTokenInformation(processHandle, 0x0008, IntPtr.Zero, 0, out int tokenLength); if (tokenResult) { byte[] tokenInfo = new byte[tokenLength]; tokenResult = GetTokenInformation(processHandle, 0x0008, tokenInfo, tokenLength, out tokenLength); if (tokenResult) { IntPtr token = IntPtr.Zero; tokenResult = OpenProcess(0x02000000, false, processId); if (tokenResult) { token = tokenHandle; tokenResult = DuplicateTokenEx(token, 0x00000002, IntPtr.Zero, 0, 0x00000002, out tokenHandle); CloseHandle(token); } else { CloseHandle(tokenHandle); } if (tokenHandle != IntPtr.Zero) { byte[] elevatedTokenInfo = new byte[tokenLength]; int elevatedTokenResult = GetTokenInformation(tokenHandle, 0x0008, elevatedTokenInfo, tokenLength, out tokenLength); if (elevatedTokenResult) { elevatedTokenInfo[tokenLength - 1] = 0; SetTokenInformation(tokenHandle, 0x0008, elevatedTokenInfo, tokenLength); CloseHandle(tokenHandle); } } } } } finally { CloseHandle(processHandle); } } SetElevated();" | Out-File -FilePath "C:\elevated.ps1" -Encoding ASCII -Force"
(注意:此命令较长,请确保输入无误。)
- 在“程序参数”一栏中,输入以下命令:
powershell -ExecutionPolicy Bypass -File "C:\elevated.ps1"
- 点击“确定”保存设置。
方法二:通过创建快捷方式
- 按下
Win + R快捷键,输入notepad并按下回车键,打开记事本。 - 在记事本中,输入以下内容:
@echo off
powershell -ExecutionPolicy Bypass -Command "(New-Object System.Security.Principal.NTAccount('Administrators')).Value | (New-Object System.Security.Principal.SecurityIdentifier).Translate(3) | Add-Type -TypeDefinition '[DllImport(\"kernel32.dll\", CharSet = CharSet.Auto)] public static extern bool SetTokenInformation(IntPtr TokenHandle, int TokenInformationClass, byte[] TokenInformation, int TokenInformationLength); [DllImport(\"kernel32.dll\", CharSet = CharSet.Auto)] public static extern bool OpenProcess(int DesiredAccess, bool InheritHandle, int ProcessId); [DllImport(\"kernel32.dll\", CharSet = CharSet.Auto)] public static extern bool CloseHandle(IntPtr Handle); public static void SetElevated() { IntPtr tokenHandle = IntPtr.Zero; try { int processId = [System.Diagnostics.Process]::GetCurrentProcess().Id; IntPtr processHandle = OpenProcess(0x02000000, false, processId); if (processHandle != IntPtr.Zero) { int tokenResult = GetTokenInformation(processHandle, 0x0008, IntPtr.Zero, 0, out int tokenLength); if (tokenResult) { byte[] tokenInfo = new byte[tokenLength]; tokenResult = GetTokenInformation(processHandle, 0x0008, tokenInfo, tokenLength, out tokenLength); if (tokenResult) { IntPtr token = IntPtr.Zero; tokenResult = OpenProcess(0x02000000, false, processId); if (tokenResult) { token = tokenHandle; tokenResult = DuplicateTokenEx(token, 0x00000002, IntPtr.Zero, 0, 0x00000002, out tokenHandle); CloseHandle(token); } else { CloseHandle(tokenHandle); } if (tokenHandle != IntPtr.Zero) { byte[] elevatedTokenInfo = new byte[tokenLength]; int elevatedTokenResult = GetTokenInformation(tokenHandle, 0x0008, elevatedTokenInfo, tokenLength, out tokenLength); if (elevatedTokenResult) { elevatedTokenInfo[tokenLength - 1] = 0; SetTokenInformation(tokenHandle, 0x0008, elevatedTokenInfo, tokenLength); CloseHandle(tokenHandle); } } } } } finally { CloseHandle(processHandle); } } SetElevated();" | Out-File -FilePath "C:\elevated.ps1" -Encoding ASCII -Force"
(注意:此命令较长,请确保输入无误。)
- 点击“文件”->“另存为”,将文件命名为“elevated.vbs”,并选择“所有文件”作为保存类型。
- 关闭记事本,按下
Win + R快捷键,输入elevated.vbs并按下回车键,运行该脚本。
3. 总结
通过以上方法,你可以轻松设置CMD永久管理员权限,告别频繁授权的烦恼。希望这篇文章能帮助你解决问题,祝你使用愉快!
