Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open an elevated cmd using command line for Windows?

People also ask

How do I open an elevated Command Prompt from the command line?

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.

How do I open an elevated Command Prompt without a Start Menu?

If you're using a keyboard with Windows 11, Windows 10, or Windows 8, you can open an elevated Command Prompt quickly from the Power User Menu. Just use the WIN+X keyboard shortcut and then select Windows Terminal (Admin) (in Windows 11) or Command Prompt (Admin) (in Windows 10/8).

How do I open an elevated terminal?

How do I run a shell in Windows Terminal in administrator mode? To run Windows Terminal with elevated administrator permission (admin mode), right-click on the Windows Terminal icon, then again on the Windows Terminal title that displays, then select "Run as administrator".

How do I open advanced Command Prompt?

Open the folder in Windows Explorer and hold the Shift key while making a right-click on the folder. Then, select 'Run command window here' to open the CMD prompt. Click on the option and you'll begin a new instance of the Command Prompt, ready and waiting at the right location!


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:

  1. Open CMD
  2. Write powershell -Command "Start-Process cmd -Verb RunAs" and press Enter
  3. A pop-up window will appear asking to open a CMD as administrator

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:

powershell -Command "Start-Process cmd -Verb RunAs"

According to documentation, the Windows security model...

does not grant administrative privileges at all times. Even administrators run under standard privileges when they perform non-administrative tasks that do not require elevated privileges.

You have the Create this task with administrative privileges option in the Create new task dialog (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:

NirCmd:

  1. Download it and unzip it.
  2. nircmdc elevate cmd

windosu:

  1. Install it: npm install -g windosu (requires node.js installed)
  2. sudo cmd

Simple way I did after trying other answers here

Method 1: WITHOUT a 3rd party program (I used this)

  1. Create a file called sudo.bat (you can replace sudo with any name you want) with following content powershell.exe -Command "Start-Process cmd \"/k cd /d %cd%\" -Verb RunAs"
  2. Move sudo.bat to a folder in your PATH; if you don't know what that means, just move these files to c:\windows\
  3. Now sudo will work in Run dialog (win+r) or in explorer address bar (this is the best part :))

Method 2: WITH a 3rd party program

  1. Download NirCmd and unzip it.
  2. Create a file called sudo.bat (you can replace sudo with any name you want) with following content nircmdc elevate cmd /k "cd /d %cd%"
  3. Move nircmdc.exe and sudo.bat to a folder in your PATH; if you don't know what that means, just move these files to c:\windows\
  4. Now sudo will work in Run dialog (win+r) or in explorer address bar (this is the best part :))

I use nirsoft programs (eg nircmdc) and sysinternals (eg psexec) all the time. They are very helpful.

But if you don't want to, or can't, dl a 3rd party program, here's another way, pure 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):

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)"


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 "

My favorite way of doing this is using PsExec.exe from SysInternals, available at 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 If the target system is Vista or higher, has the process run with the account's elevated token, if available.