如何使用 Windows 命令行打开提升的 cmd?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19098101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open an elevated cmd using command line for Windows?
提问by user2633882
How do I open a elevated command prompt using command lines on a normal cmd?
如何在普通 cmd 上使用命令行打开提升的命令提示符?
For example, I use runas /username:admin cmd
but the cmd that was opened does not seem to be elevated! Any solutions?
比如我用runas /username:admin cmd
但是打开的cmd好像没有提升!任何解决方案?
回答by Gui
I ran into the same problem and the only way I was able to open the CMD as administrator from CMD was doing the following:
我遇到了同样的问题,我能够从 CMD 以管理员身份打开 CMD 的唯一方法是执行以下操作:
- Open CMD
- Write
powershell -Command "Start-Process cmd -Verb RunAs"
and press Enter - A pop-up window will appear asking to open a CMD as administrator
- 打开命令
- 写
powershell -Command "Start-Process cmd -Verb RunAs"
和按Enter - 将出现一个弹出窗口,要求以管理员身份打开 CMD
回答by Ricardo Stuven
According to documentation, the Windows security model...
根据文档,Windows 安全模型...
does not grant administrative privileges at all times. Even administrators run under standard privilegeswhen they perform non-administrative tasks that do not require elevated privileges.
始终不授予管理权限。即使管理员在执行不需要提升权限的非管理任务时,也会在标准权限下运行。
You have the Create this task with administrative privilegesoption in the Create new taskdialog (Task Manager > File > Run new task), but there is no built-in way to effectively elevate privileges using the command line.
您可以在创建新任务对话框(任务管理器 > 文件 > 运行新任务)中使用管理权限创建此任务选项,但没有使用命令行有效提升权限的内置方法。
However, there are some third party tools (internally relying on Windows APIs) you can use to elevate privileges from the command line:
但是,您可以使用一些第三方工具(内部依赖 Windows API)从命令行提升权限:
NirCmd:
指令:
- Download it and unzip it.
nircmdc elevate cmd
- 下载并解压。
nircmdc elevate cmd
windosu:
温多苏:
- Install it:
npm install -g windosu
(requires node.jsinstalled) sudo cmd
- 安装它:(
npm install -g windosu
需要安装node.js) sudo cmd
回答by Stiegler
I don't have enough reputation to add a comment to the top answer, but with the power of aliases you can get away with just typing the following:
我没有足够的声誉来为最佳答案添加评论,但是借助别名的力量,您只需键入以下内容即可:
powershell "start cmd -v runAs"
This is just a shorter version of user3018703 excellent solution:
这只是 user3018703 优秀解决方案的较短版本:
powershell -Command "Start-Process cmd -Verb RunAs"
回答by Dheeraj Bhaskar
Simple way I did after trying other answers here
我在这里尝试其他答案后所做的简单方法
Method 1: WITHOUT a 3rd party program (I used this)
方法 1:没有第 3 方程序(我用过这个)
- Create a file called
sudo.bat
(you can replacesudo
with any name you want) with following contentpowershell.exe -Command "Start-Process cmd \"/k cd /d %cd%\" -Verb RunAs"
- Move
sudo.bat
to a folder in yourPATH
; if you don't know what that means, just move these files toc:\windows\
- Now
sudo
will work in Run dialog (win+r) or in explorer address bar (this is the best part :))
- 使用以下内容创建一个名为
sudo.bat
(您可以替换sudo
为您想要的任何名称)的文件powershell.exe -Command "Start-Process cmd \"/k cd /d %cd%\" -Verb RunAs"
- 移动
sudo.bat
到您的文件夹中PATH
;如果您不知道这意味着什么,只需将这些文件移动到c:\windows\
- 现在
sudo
将在运行对话框 ( win+r) 或资源管理器地址栏中工作(这是最好的部分 :))
Method 2: WITH a 3rd party program
方法 2:使用第 3 方程序
- Download NirCmdand unzip it.
- Create a file called
sudo.bat
(you can replacesudo
with any name you want) with following contentnircmdc elevate cmd /k "cd /d %cd%"
- Move
nircmdc.exe
andsudo.bat
to a folder in yourPATH
; if you don't know what that means, just move these files toc:\windows\
- Now
sudo
will work in Run dialog (win+r) or in explorer address bar (this is the best part :))
- 下载NirCmd并解压。
- 使用以下内容创建一个名为
sudo.bat
(您可以替换sudo
为您想要的任何名称)的文件nircmdc elevate cmd /k "cd /d %cd%"
- 移动
nircmdc.exe
和sudo.bat
到您的文件夹中PATH
;如果您不知道这意味着什么,只需将这些文件移动到c:\windows\
- 现在
sudo
将在运行对话框 ( win+r) 或资源管理器地址栏中工作(这是最好的部分 :))
回答by john v kumpf
I use nirsoft programs (eg nircmdc) and sysinternals (eg psexec) all the time. They are very helpful.
我一直使用 nirsoft 程序(例如 nircmdc)和 sysinternals(例如 psexec)。他们非常有帮助。
But if you don't want to, or can't, dl a 3rd party program, here's another way, pure Windows.
但是,如果您不想或不能 dl 一个 3rd 方程序,这是另一种方式,纯 Windows。
Short answer: you can while elevated create a scheduled task with elevated privileges which you can then invoke later while not elevated.
简短回答:您可以在提升时创建一个具有提升权限的计划任务,然后您可以稍后在未提升的情况下调用该任务。
Middle-length answer: while elevated create task with (but I prefer task scheduler GUI):
中等长度的答案:虽然提升创建任务(但我更喜欢任务调度程序 GUI):
schtasks /create /sc once /tn cmd_elev /tr cmd /rl highest /st 00:00
Then later, no elevation needed, invoke with
然后,不需要提升,调用
schtasks /run /tn cmd_elev
Long answer: There's a lot of fidgety details; see my blog entry "Start program WITHOUT UAC, useful at system start and in batch files (use task scheduler)"
长答案:有很多烦躁的细节;请参阅我的博客条目“在没有 UAC 的情况下启动程序,在系统启动和批处理文件中很有用(使用任务调度程序)”
回答by statler
The following as a batch file will open an elevated command prompt with the path set to the same directory as the one from where the batch file was invoked
以下作为批处理文件将打开一个提升的命令提示符,其路径设置为与调用批处理文件的目录相同的目录
set OLDDIR=%CD%
powershell -Command "Start-Process cmd -ArgumentList '/K cd %OLDDIR%' -Verb RunAs "
回答by David Rodriguez
My favorite way of doing this is using PsExec.exe from SysInternals, available at http://technet.microsoft.com/en-us/sysinternals/bb897553
我最喜欢的方法是使用来自 SysInternals 的 PsExec.exe,可从http://technet.microsoft.com/en-us/sysinternals/bb897553 获得
.\psexec.exe -accepteula -h -u "$username" -p "$password" cmd.exe
The "-h" switch is the one doing the magic:
“-h”开关是一个神奇的开关:
-h If the target system is Vista or higher, has the process run with the account's elevated token, if available.
-h 如果目标系统是 Vista 或更高版本,则使用帐户的提升令牌运行进程(如果可用)。
回答by Stefan Gadecki
Make the batch file save the credentials of the actual administrator account by using the /savecred
switch. This will prompt for credentials the first time and then store the encrypted password in credential manager. Then for all subsequent times the batch runs it will run as the full admin but not prompt for credentials because they are stored encrypted in credential manager and the end user is unable to get the password. The following should open an elevated CMD with full administrator privileges and will only prompt for password the first time:
使用/savecred
开关使批处理文件保存实际管理员帐户的凭据。这将首次提示输入凭据,然后将加密的密码存储在凭据管理器中。然后,在批处理运行的所有后续时间中,它将以完全管理员身份运行,但不会提示输入凭据,因为它们以加密方式存储在凭据管理器中,最终用户无法获取密码。以下应该打开具有完全管理员权限的提升的 CMD,并且只会在第一次提示输入密码:
START c:\Windows\System32\runas.exe /user:Administrator /savecred cmd.exe
回答by seangwright
I've been using Elevate
for awhile now.
我已经使用Elevate
了一段时间了。
It's description - This utility executes a command with UAC privilege elevation. This is useful for working inside command prompts or with batch files.
这是描述—— This utility executes a command with UAC privilege elevation. This is useful for working inside command prompts or with batch files.
I copy the bin.x86-64\elevate.exe
from the .zip
into C:\Program Files\elevate
and add that path to my PATH
.
我bin.x86-64\elevate.exe
从.zip
进入复制C:\Program Files\elevate
并将该路径添加到我的PATH
.
Then GitBash I can run something like elevate sc stop W3SVC
to turn off the IIS
service.
然后我可以运行 GitBashelevate sc stop W3SVC
来关闭IIS
服务。
Running the command gives me the UAC
dialog, properly focused with keyboard control and upon accepting the dialog I return to my shell.
运行该命令为我提供了UAC
对话框,通过键盘控制正确聚焦,并在接受对话框后返回到我的外壳。
回答by jOte-
..
..
@ECHO OFF
SETLOCAL EnableDelayedExpansion EnableExtensions
NET SESSION >nul 2>&1
IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
GOTO :EOF
:ELEVATE
SET this="%CD%"
SET this=!this:\=\!
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('CMD', '/K CD /D \"!this!\"', '', 'runas', 1);close();"
EXIT 1
save this script as "god.cmd" in your system32 or whatever your path is directing to....
将此脚本保存为 system32 中的“god.cmd”或您的路径指向的任何内容....
if u open a cmd in e:\mypictures\ and type god it will ask you for credentials and put you back to that same place as the administrator...
如果你在 e:\mypictures\ 中打开一个 cmd 并输入 God 它会要求你提供凭据并将你放回与管理员相同的位置...