22

Is it possible to switch from normal mode to administrator mode in command line? I don't want to open command line by right clicking and selecting "run as administrator". Thanks

7 Answers 7

11

The answer is no. The "full administrative token" is associated with an .EXE when it's launched. You need to start a new CMD.EXE to get full-admin privs. To make UAC a bit more bearable, you can enable "auto-admin approval" through group policy.

10
  • 1
    To make UAC more bearable you'd have to disable it entirely. Sep 14, 2012 at 20:06
  • 4
    @AnsgarWiechers: I hope this comment was not serious. It seems a bit lame to encourage anyone to disable UAC completely.
    – Sk8erPeter
    Dec 31, 2013 at 10:15
  • 3
    @Sk8erPeter I am quite serious about this. If you're aiming for a secure system: disable UAC and use separate accounts (an admin account for administrative tasks and an account without administrative privileges for day-to-day work). UAC is just a lame workaround that Microsoft invented so that their customers can remain members of the administrators group all the time without actually having admin privileges all the time. Jan 3, 2014 at 10:26
  • 1
    @Sk8erPeter As I already said: the solution to the problem is using separate accounts for administrative and non-administrative tasks (a solution that has been working since Windows 2000 at least, and on Unix even longer than that). UAC is just a lame way of working around the issue and making everything more complicated in the process. And sudo is something entirely different from UAC as it allows to selectively grant privileged execution of particular commands on particular hosts to particular groups/users. Jan 4, 2014 at 12:03
  • 1
    @Sk8erPeter *sigh* The average user is already being forced to create separate accounts during the installation process (and has been for more than a decade). Only that Microsoft chose to make that additional user a member of the Administrators group as well rather than doing The Right Thing(tm). Anyway, I don't think SF is the right place for this kind of discussion, so I'll excuse myself from it. Jan 4, 2014 at 13:23
21

Or from within Powershell:

Start-Process powershell -Verb runas
3
  • My powershell(on Win7) doesn't have the runas verb... should it? I checked with 'verb | findstr /B /C:"R"'. Runas isn't on the list.
    – Totem
    Jan 26, 2015 at 12:33
  • For me, this is the most valuable answer, but I have to choose the "No" answer as the right one, because there is no possibility to switch the user modes. Thank you, Chad.
    – culter
    Jan 17, 2018 at 15:05
  • How do you start the new powershell in the same path as the one into which you type this command? It opens in a default path instead of in the path where I was. Aug 4, 2021 at 15:14
1

How about this:

runas /user:administrator cmd.exe
1
  • 1
    To my understanding, this requires the actual Administrator account to be active (Enter the password for administrator:). It is not identical with simply running the command prompt with elevated privileges. Sep 14, 2012 at 14:33
1

You can't really "switch" between the two modes, but there are some 3rd party tools that let you launch programs from the command line in the elevated admin. Provided you drop them into a working path, you could, for example, use "elevate cmd" from the start menu or from an existing command prompt to start a new session in the elevated mode.

1

You can have a shortcut to cmd.exe and just modify the Advanced properties tab to "Run as Administrator".

1

if you are in visual studio code, in configuration you can add this:

    "terminal.integrated.shellArgs.windows": [
        "-Verb runas"
    ],
1

I managed to do it as follows. I put the following line on top, which will restart the script with administrator rights (in a separate window) via PowerShell, if the cmd had not already been started as administrator. Aesthetically it's not perfect, but it does the job.

net.exe session 1>NUL 2>NUL || (powershell "Start-Process -FilePath Cmd.exe -ArgumentList \"/c %~dpnx0\" -Verb RunAs" & goto :eof)

You must log in to answer this question.

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