How to run an application as "run as administrator" from the command prompt?

824,984

Solution 1

Try this:

runas.exe /savecred /user:administrator "%sysdrive%\testScripts\testscript1.ps1" 

It saves the password the first time and never asks again. Maybe when you change the administrator password you will be prompted again.

Solution 2

See this TechNet article: Runas command documentation

From a command prompt:

C:\> runas /user:<localmachinename>\administrator cmd

Or, if you're connected to a domain:

C:\> runas /user:<DomainName>\<AdministratorAccountName> cmd

Solution 3

It looks like psexec -h is the way to do this:

 -h         If the target system is Windows Vista or higher, has the process
            run with the account's elevated token, if available.

Which... doesn't seem to be listed in the online documentation in Sysinternals - PsExec.

But it works on my machine.

Share:
824,984
Praveen Jakkaraju
Author by

Praveen Jakkaraju

Updated on December 30, 2020

Comments

  • Praveen Jakkaraju
    Praveen Jakkaraju over 3 years

    I have a batch file called test.bat. I am calling the below instructions in the test.bat file:

    start /min powershell.exe %sysdrive%\testScripts\testscript1.ps1
    

    When I run this through the command prompt, my testscript is running successfully. I want to run it as administrator (as if I have created a desktop shortcut and run as administrator. It shouldn't prompt for any username or password).

    I have tried adding /elevate and /NOUAC parameters in the above test.bat, but no luck. How do I fix this issue?

    I know how to do it manually, but I want this to be executed from the command prompt.

    (By Marnix Klooster): ...without using any additional tools, like those suggested in an answer to Super User question How to run program from command line with elevated rights.)

  • John Ruiz
    John Ruiz over 12 years
    I re-read your question and you don't want to be prompted. My suggestion will cause a password prompt. Sorry!
  • Praveen Jakkaraju
    Praveen Jakkaraju over 12 years
    i tried this command, it is asking for credential, it should not ask for credentials. as i mentioned in my query. if i right click the desktop item run it as an administrator is the expected behaviour.
  • ygoe
    ygoe almost 11 years
    Doesn't work here. Instead I just get the help output.
  • Vince
    Vince about 10 years
    This doesn't seem to work for me unless I run it from a process which is already has admin privileges: "Couldn't install PSEXESVC service"
  • BrainSlugs83
    BrainSlugs83 over 8 years
    I don't even have this command in Windows 10. -- Not from the command prompt, or from Power Shell.
  • Ben Voigt
    Ben Voigt over 8 years
    While this may work correctly, it's an absolutely horrible idea as it undermines the whole concept of UAC.
  • Daniel Sokolowski
    Daniel Sokolowski about 8 years
    @BenVoigt please suggest an alternative then
  • Julien Kronegg
    Julien Kronegg about 8 years
    Try powershell -Command "Start-Process 'C:\program.exe' -Verb runAs" (replace C:\program.exe by your command), see superuser.com/questions/55809/…
  • RayLoveless
    RayLoveless almost 8 years
    Why does it require me to enter a password when I can right click any other program and run as admin without entering a password? Thanks
  • arkon
    arkon almost 8 years
    @BenVoigt Don't blow a gasket. Like any power command, it is both useful and necessary in specific contexts.
  • Dime
    Dime over 7 years
    Here is what did eventually work for me very well: superuser.com/questions/262571/…
  • solstice333
    solstice333 over 7 years
    @RayLoveless I believe the difference is that right clicking and running as admin runs the program under the same user but elevated whereas using runas has the ability to run the program under a completely different user i.e. perhaps the "administrator" account.
  • Yash Kumar Verma
    Yash Kumar Verma over 6 years
    any method using which I can just chip in the admin password in the initian command itself. It asks for admin password when i run the initial command. Can that input be provided in the first place ? runas.exe /user:yash a.exe MyAwesomePassword
  • Aisah Hamzah
    Aisah Hamzah over 6 years
    @BrainSlugs83, you have to download it, the link is in the answer.
  • undrline - Reinstate Monica
    undrline - Reinstate Monica about 5 years
    This runs as the user Administrator, which is not the same as running with administrative rights. This is also the same as the other answer written prior, but with the spin of saving credentials.
  • user3177026
    user3177026 almost 5 years
    It is true that this answer undermines the whole concept of UAC and that you'll run as Administrator rather than administrative rights, anyway, if you trust this and you want moreover use a blank password for the Administrator, then add the following to the registry: [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa] "LimitBlankPasswordUse"=dword:00000000
  • Jason C
    Jason C almost 3 years
    @BenVoigt If you're sitting at my laptop in a command prompt with an evil grin, I've got bigger problems than a philosophically undermined UAC.
  • Ben Voigt
    Ben Voigt almost 3 years
    @JasonC: I'm less concerned about the person with physical access to my computer, which I can control, and more about having a zero-day exploit in a web browser. Even if you only visit legit websites, you are opening yourself up to attack from any untrustworthy party they sell ad space to. Including right here When that attack happens, UAC is one of very few OS features left protecting you.
  • Jason C
    Jason C almost 3 years
    @BenVoigt 🤷‍♂️ Meh, all my passwords and credit card numbers are conveniently stored across dozens of data centers and backups across the world, tied to a single user name and password, managed by a company addicted to treadmill desks, bad graphic design, and information, and guarded by surly, overworked, caffeine-powered individuals that I've never met but for some reason implicitly trust. And all my sensitive documents are on this thumb drive in my pock.... actually I don't know where it is, huh. So I'm safe. The Chinese zero-day hackers can have my Simpsons porn. I've got cloud backups. 😁
  • Redoman
    Redoman over 2 years
    Why using this command with my administrator username it still says I am not authorized to modify files in "c:\program files" folder?