114

As a developer, I often have the need to open a command prompt for various purposes. For example, I use iisreset to restart my local web server.

I typically open the command window in one of two ways:

  1. Press Win, type "cmd" and press Enter
  2. While in Explorer, hold Shift and right click on a folder, and choose "Open command window here"

However, when I open the command window in either of these ways, I do not have the full administrator privileges that I feel I am entitled to.

I am an administrator, but cmd.exe doesn't know that

In order to run administrator-only applications, I have to open the command line in this relatively laborious way:

  1. Press Win, type "cmd"
  2. Wait for the menu to populate
  3. Lift hand off the keyboard and put it on the mouse
  4. Right click the "cmd.exe" menu item
  5. Choose "Run as administrator"

This is unacceptable for several reasons:

  • The window always opens in C:\windows\system32, rather than my Users directory (as in approved technique 1) or the folder I want to be in (as in approved technique 2). So I often have to change directories to get where I want to go.
  • This process is several milliseconds slower than either of my preferred methods. Performed several times a day, every day for the remainder of my career, it adds up to about ten days of lost time spent clicking and waiting and directory changing.
  • As a programmer, performing a pointless robotic task causes me great emotional pain.
  • As a programmer, lifting my hand is a strenuous task that causes me great physical pain.

That is why I am looking for a one-and-done solution that will let approved techniques 1 & 2 open administrator command prompts.

Many programs let you permanently change their default privilege level from the Properties menu. Command Prompt is not one of those programs.

enter image description here

How do I make the command window run as administrator by default (even when using the "Open command window here" context menu option)?

3
  • Why not just change UAC to elevate only?
    – surfasb
    Jul 26, 2012 at 2:46
  • I don't see an "elevate only" option on my UAC slider. Are you referring to something besides the User Account Control Settings window?
    – Kevin
    Jul 26, 2012 at 11:37
  • 11
    I think its Hammer Time... Sep 23, 2014 at 8:42

14 Answers 14

99

To answer the first part of your question, when you hit the Windows key and type "CMD" you can hit Ctrl + Shift + Enter to open as administrator.

To answer the second part of your question, paste the following into notepad and save it with a ".reg" extension:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\runas]
@="Open Command Window Here as Administrator"

[HKEY_CLASSES_ROOT\Directory\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""

Then run the file. It will merge the changes into the registry and add the option to your context menus. (No shift key needed.)

6
  • 8
    Note; you will still have to accept the UAC prompt in both cases.
    – Synetech
    Jul 25, 2012 at 17:17
  • 1
    Registry edit doesnt work in windows 8.
    – Biker John
    Aug 1, 2014 at 11:50
  • Registry patch works for Server 2012 R2
    – fiat
    Aug 21, 2014 at 0:51
  • It did not work for me on Windows 7 Mar 10, 2015 at 23:25
  • 1
    If you don't see the command after running the reg file, log out of Windows and log back in.
    – trebormf
    Jan 23, 2016 at 16:58
46

Many programs let you permanently change their default privilege level from the Properties menu. Command Prompt is not one of those programs.

The Compatibility tab is completely disabled for all of Windows’ executables:

enter image description here

To set admin privileges for Windows executables, you need to create a shortcut and use Shortcut tab→Advanced (the command-prompt item in the Start menu is already a shortcut):

enter image description here

The window always opens in C:\windows\system32, rather than my Users directory (as in approved technique 1) or the folder I want to be in (as in approved technique 2). So I often have to change directories to get where I want to go.

That is normal and makes sense since if you are opening an admin command-prompt, you are probably doing some system actions for which you need admin privileges instead of user actions that you already have permissions for anyway.

You can set the default directory globally by adding/editing the Autorun registry entry (it does not even have to be an expandable string to use environment variables):

REGEDIT4
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"Autorun"="cd /d \"%userprofile%\""

Now, any time you open a command-prompt using any method will automatically default (well, technically change-directory) to your user-profile directory.

Even better, you can add other commands to be automatically run whenever you open a command-prompt using the & operator (e.g., cd /d %userprofile% & cls & dir). In addition, you can set the same value in the same key under the HKLM branch to set it for all users.


Note that you will still have to accept the UAC prompt. Unfortunately there is (currently?) no way to create a UAC “whitelist” of trusted programs so that the command-prompt can be run as admin without having to accept the prompt. This leaves you with (a) few options.

  • You can turn UAC down or off altogether (useraccountcontrolsettings.exe)
  • You can use a privilege-elevation program like Elevator aka Elevate Me or the Elevation PowerToy
  • Create an elevated scheduled task, enter the credentials (once) for it, and then create a shortcut to the task

In the first case, you avoid the UAC altogether while in the latter two, you only enter your credentials once when creating the shortcut.

3
  • There's a problem with setting the current directory via autorun. With that value set, cd /d "%userprofile%" autoruns not just when opening a new console window, but also every time a new thread of execution is invoked. This can cause problems, for example, with for /f loops. Try this at a cmd prompt: cd "\Program Files" then for /f "delims=" %I in ('cmd /c cd') do @echo %I. You'll see the for /f loop thinks its current working directory is $home, not Program Files. It's safer to modify the cmd shortcut and set the target as "%comspec%" /k cd /d "%userprofile%".
    – rojo
    Aug 29, 2015 at 4:41
  • If you want to preserve the Windows version and copyright info when opening a new console window, set the shortcut's target to %comspec% /c cd /d "%USERPROFILE%" & %comspec%
    – rojo
    Aug 29, 2015 at 5:37
  • 1
    +1 for non-registry editing answer. The other answer might have been fine in 2012, but I don't want to try it on Windows 10 today.
    – Alan
    Oct 24, 2017 at 14:35
21

The solution I use is to open command prompt by opening start menu, typing 'cmd' and pressing Ctrl+Shift+Enter. This will cause the selected program to be launched as administrator.

7
  • 1
    Is there an alternative that doesn't require "search" ? (because from what I know typing stuff in the box actually does searching)
    – Pacerier
    Nov 13, 2011 at 12:36
  • Only other way I know is Luke's answer, disable UAC. The searching for cmd is pretty fast, can't even manage to hit Ctrl+Shift+Enter before it finds it.
    – Dracs
    Nov 13, 2011 at 12:45
  • I do that all the time on customer computers. It finds CMD pretty damn fast Nov 13, 2011 at 18:04
  • Will this load it as Administrator?
    – barlop
    Nov 13, 2011 at 20:28
  • @barlop Yes, that hotkey (Ctrl+Shift+Enter) will launch the highlighted program in the Start menu as administrator.
    – Dracs
    Nov 13, 2011 at 22:01
17

To auto-run CMD as admin each time it is simply clicked (without having to right-click or create additional shortcuts), there is an easy fix for this:

  1. In the Registry Editor, navigate to:
    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

    • If there isn't a Layers folder, you'll have to create one and name it:
      Right-click the AppCompatFlags folder select NewKey
  2. Right click Layers, select NewString Value. Set the Name of your new string value to the full path to (CMD.EXE) command prompt (i.e. C:\Windows\System32\cmd.exe)

  3. Double-click the new string value to modify it, and set the "Value data" to RUNASADMIN.

I had the same annoyance, and this worked for me.

0
12

If you want CMD.exe be always run as administrator, just add bellow reg code to your Registry:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Windows\\System32\\cmd.exe"="~ RUNASADMIN"
"C:\\Windows\\SysWOW64\\cmd.exe"="~ RUNASADMIN"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Windows\\System32\\cmd.exe"="~ RUNASADMIN"
"C:\\Windows\\SysWOW64\\cmd.exe"="~ RUNASADMIN"
4
  • 2
    This worked for me on Windows 8.1. Now I just hit Windows-R, cmd, Enter. It also works for opening a command window from File Explorer via Shift-RightClick > Open command window here.
    – Troy Gizzi
    Jun 11, 2015 at 13:54
  • 1
    If this works, this is the correct answer, despite the much longer version currently accepted.
    – Xodarap777
    Nov 10, 2015 at 18:41
  • This works for Windows 10 too. Put the content into notepad, save it with a .reg extension and run it Oct 31, 2017 at 0:15
  • Didn't work for me. HKLM part didn't even register, for some reason. Debate on whether this still works: superuser.com/questions/961573/…, with alternatives. Oct 20, 2019 at 20:59
4

There have been a couple tools that I've used that were other workarounds (in addition to what Jesse mentioned).

Back when Vista first came out, Microsoft came out with an "elevate" powerToy that was quite useful and did the job and still, I believe (haven't tested) works for Windows 7. Drop it into your path environment and you could run "elevate iisreset" from a normal command prompt (or even "elevate cmd" from the start/run or a create a shortcut)

1
4

Just copy & paste cmd.exe and rename it as "cmda.exe" in \system32\ folder (337 KB isn't even a big deal...). The "run as administrator" option in cmda.exe's properties will no longer grayed out and you can set it to always run as admin. You'll now have two command prompts: first one is regular, 2nd one has an (a)dmin privilege. It's as easy to type "cmda" in Win search bar as you do with "cmd"!

For the cmd in context menu with admin privilege, ensure that you already have cmda.exe in place, add this into new .reg file & launch it

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\cmda]
@="Open command window here (Administrator)"
"HasLUAShield"=""
"Extended"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\cmda\Command]
@="cmda.exe /s /k pushd \"%V\""

You will see two same lines in context menu (shift); one non-elevated Command Prompt and another one with "(Administrator)" added in.

That's it...

4

The easiest way to bypass all the grief of UAC etc is to setup a Task Schedule entry to run cmd.exe using the highest privileges. Do not create a Trigger event for it and make sure that the Allow Task To Be Run On Demand check box is selected in the Settings tab. Also in the Settings tab make sure that the "If The Task Is Already Running, Then The Following Rule Applies" drop down is set to Do Not Start A New Instance.

Then create a shortcut to this on your desktop and run it from there, or via a shortcut hotkey or pin the shortcut to the Task bar.

I have done it on my Windows 7 system and it works perfectly.

2
  • 1
    Oops, I forgot to specify one important thing. Here is the target path for the shortcut to the Task. C:\Windows\System32\schtasks.exe /run /TN "command prompt without UAC". The text inside the quotes is whatever you name the Task when you create the Task. Sep 25, 2013 at 1:36
  • don't enclose your email id in your answers Sep 25, 2013 at 4:07
1

Create a shortcut to cmd.exe, right click and select properties, on the Shortcut tab select Advanced, check Run as Administrator.

1

I think the easiest way to always run cmd as admin is to make the .reg file with the code below:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
@="C:\\Windows\\System32\\cmd.exe"="RUNASADMIN"

Paste that in notepad or something and save as .reg file. Note: This makes command prompt run as admin for all users. To always run as admin only on your user just change HKEY_LOCAL_MACHINE to HKEY_CURRENT_USER

1

All thanks to Jesse Brohinsky

A more improved version. This also includes the background directory that enables you to opencmd from inside the folder aswell. Also for the sake of it you can open the commandpromt as non admin.

Also you can disable AdminApprovalMode. This way you dont need to run as admin when you are admin. http://helpdeskgeek.com/windows-7/turn-off-admin-approval-mode-in-windows-7/

Windows Registry Editor Version 5.00
;write minus before to unreg example: [-HKEY_CLASSES_ROOT\Directory\shell\cmdhere]

[HKEY_CLASSES_ROOT\Directory\shell\cmdhere]
@="Open Command Window Here"

[HKEY_CLASSES_ROOT\Directory\shell\cmdhere\command]
@="cmd.exe /s /k pushd \"%V\""

[HKEY_CLASSES_ROOT\Directory\Background\shell\cmdhere]
@="Open Command Window Here"

[HKEY_CLASSES_ROOT\Directory\Background\shell\cmdhere\command]
@="cmd.exe /s /k pushd \"%V\""

[HKEY_CLASSES_ROOT\Directory\shell\runas]
@="Open Command Window Here as Administrator"

[HKEY_CLASSES_ROOT\Directory\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""

[HKEY_CLASSES_ROOT\Directory\Background\shell\runas]
@="Open Command Window Here as Administrator"

[HKEY_CLASSES_ROOT\Directory\Background\shell\runas\command]
@="cmd.exe /s /k pushd \"%V\""
0

The only other way, other than what Dracs mentioned, is to disable UAC completely

0
0

The following works for me

@echo off
:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

you will get the same uac prompt confirming to runas admin.. found it some time ago @ stackexchange

-1

You can try this.

Save the code as a reg file in windows 10 or 11 and execute it as admin.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
@="C:\\Windows\\System32\\cmd.exe"="RUNASADMIN"
1
  • This is just a variation of at least THREE existing answers on this question page. I appreciate your effort, but your answer isn't needed
    – gregg
    Dec 2, 2021 at 19:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .