On Windows 7, is there a command prompt line that can start cmd as an administrator?

39

14

On Linux, we can use

sudo bash

to run a shell as the superuser.

On Windows 7, we can use

Start menu -> (in search box, type) cmd -> Right click on search result and choose Run as Administrator

to run a command prompt as an administrator. Is there also a command prompt line that is like Linux's sudo or su so as to start a shell as an administrator?

nonopolarity

Posted 2009-11-24T01:48:53.597

Reputation: 7 932

Question was closed 2014-02-22T08:13:09.597

11You can also search for cmd (like you already do) and press "ctrl+shift+enter" to launch the selected program as administrator. – Joshua – 2009-11-24T02:18:00.957

Answers

34

The runas command is the closest thing to what you're looking for:

runas /user:username cmd.exe

The format is runas, the username you want to run as, other options, and then the program you want to run.

If you're on a domain, you can use:

runas /user:DOMAIN\USERNAME cmd.exe

This can be run from the Run box ( Win+R ) or from a command window.

Note, however, that this is not exactly like sudo - you can't use it to elevate yourself, just run something as a different user. If you're already have administrative rights, a runas to your account will give you the same access as the regular command (unless, of course, you runas a different administrator account, which will grant those administrator's rights to the new shell).

Jared Harley

Posted 2009-11-24T01:48:53.597

Reputation: 11 692

But what is the administrator password? It doesn't accept a blank one. If I right-click > Run as administrator, it doesn't ask me for a password (possibly because my user account has admin privileges, but this is different from run as admin, which will run with elevated rights). – CodeManX – 2015-08-21T13:55:45.313

2It appears that by default the administrator does not have a password. And that the account is actually disabled. The administrator account needs to be first enabled, given a password and then it becomes an actual user that you can log into and runas into. – CMCDragonkai – 2016-03-24T14:49:42.383

+1, and for local admin runas /user:administrator cmd.exe is not hard to remember after a dozen types or so. All it is is the command itself runas followed by the /user: switch, followed by the user we want to run it as, then finally the program we want to run, cmd.exe (the command prompt in our case). This is as close to a one line you can get for running command prompts elevated. Also, if you do start->run sometimes there's a checkbox to run the program elevated, I haven't put any effort into figuring out what makes it appear, but it is an option. – MDMoore313 – 2013-03-09T05:31:08.070

2Also, unlike sudo, runas will never cache the password for any length of time. So if you were planning on queuing up a couple quick commands like you can with successive runs of sudo. So just open a console window and run your commands there. – Joshua – 2009-11-24T02:26:29.893

so the shortest form is "runas /user:DOMAIN\Administrator cmd.exe" wow... that's pretty long and hard to remember... and i can create an alias, but what if i am on someone else's computer and helping out? will it be a good idea if MS can create a special command called runasadmin? – nonopolarity – 2009-11-24T09:23:08.397

5Joshua: Unless you use /savecred. – user1686 – 2009-11-24T18:19:17.400

11

Try creating a shortcut to cmd.exe. Right Click > Properties. Click on Advanced button. And tick the box "Run as Administrator" and click OK.

Assign it a shortcut key if you want.

And you could also assign the default location for the CMD prompt to start in.. eg %CD%

Methical

Posted 2009-11-24T01:48:53.597

Reputation: 194

8

Another alternative is to click Start and type cmd, followed by Ctrl + Shift + Enter

Source: Microsoft Technet

hanxue

Posted 2009-11-24T01:48:53.597

Reputation: 2 354

3

Use SuRun.

It works on Windows 8 as well. It runs the application in "admin" mode without a prompt, yet you have greater control on what applications are run (you can see it on the SuRun's control panel applet). It works great in a batch file too...

Kippax

Posted 2009-11-24T01:48:53.597

Reputation: 31

just a note for Windows 8 (and 8.1): I was not able to launch a command prompt as admin, because surun replaces - by default settings - the built-in runas function in the context menus. – Wolf – 2015-01-15T11:18:51.077

1

If you want a command-line solution: download hstart from http://www.ntwind.com/software/hstart.html and extract hstart.exe or hstart64.exe to somewhere on your path. You can start an elevated command prompt with hstart /runas cmd or hstart64 /runas cmd.

But the fastest way (as noted in a comment for the question) is still to press Windows, type cmd and press Ctrl + Shift + Enter.

Luke

Posted 2009-11-24T01:48:53.597

Reputation: 1 025

1

Create a batch file such as sudo.bat, and type the following commands and then save in the PATH:

@echo Set objShell = CreateObject("Shell.Application") > %temp%\sudo.tmp.vbs
@echo args = Right("%*", (Len("%*") - Len("%1"))) >> %temp%\sudo.tmp.vbs
@echo objShell.ShellExecute "%1", args, "", "runas" >> %temp%\sudo.tmp.vbs
@cscript %temp%\sudo.tmp.vbs

To use it, for an instance, sudo cmd net start fms, however, the script won't work if there's quotemark in command line.

Cosmore

Posted 2009-11-24T01:48:53.597

Reputation: 115

2This results in prompting user with UAC dialog box, and then running the desired command in a separate window. – TOOGAM – 2015-02-03T19:45:52.163

1

I was frustrated with the existing solutions for this, so I wrote a little Node.js script which should be more familiar to *nix users.

It elevates the current user with no password prompts (just UAC), redirects stdin and stdout, and runs in the same console window.

https://github.com/tehsenaus/windosu

teh_senaus

Posted 2009-11-24T01:48:53.597

Reputation: 141

This works great - just say sudo <command> and hit Alt+Y in the UAC dialog. – Brian Burns – 2018-06-03T11:42:10.353

0

The Open Command Prompt Shell Extension can add this capability for you as well.

afrazier

Posted 2009-11-24T01:48:53.597

Reputation: 21 316

0

I include genuine su and sudo commands with my Hamilton C shell to run a command as another user or elevated (with the UAC prompt) or both, i.e., elevated as another user. Unlike runas and other alternatives, my su and sudo know how to pass current directories, environment variables and long command lines. This is a commercial product but there is a free version. Full disclosure: I'm the author. But I'm also happy to answer questions.

Nicole Hamilton

Posted 2009-11-24T01:48:53.597

Reputation: 8 987

0

You might like Sudo for Windows.

Tomáš Kafka

Posted 2009-11-24T01:48:53.597

Reputation: 214