How to run an application as "run as administrator" from the command prompt? [closed]
Asked Answered
S

3

149

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

Sines answered 23/11, 2011 at 22:5 Comment(6)
possible duplicate of Run Batch file as administrator - Windows 7 - Command "Run As" from network file systemInterviewee
This question is OK to ask here and should not be closed as off topic. It's a programming and software development related question. DotNet program and C++ all have ability to iterative with system and other programs by invoke console command or simply through a batch file.Sisneros
This was useful for me: superuser.com/questions/55809/… Sometimes "run as administrator" is expected to mean "run with elevated rights".Overcasting
sudo.bat might be useful to someone. pastebin.com/taHp4FzRCrossgrained
Read more for creating a vbscript to do that for you : ss64.com/vb/syntax-elevate.htmlDwaindwaine
Should be moved to superuser instead of closed (and also flagged as duplicate of superuser.com/questions/55809/… )Cowrie
P
103

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.

Pyrostat answered 13/9, 2012 at 7:4 Comment(14)
While this may work correctly, it's an absolutely horrible idea as it undermines the whole concept of UAC.Thamos
@BenVoigt please suggest an alternative thenGrandeur
Try powershell -Command "Start-Process 'C:\program.exe' -Verb runAs" (replace C:\program.exe by your command), see superuser.com/questions/55809/…Cowrie
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? ThanksVirtuosity
@BenVoigt Don't blow a gasket. Like any power command, it is both useful and necessary in specific contexts.Variety
Here is what did eventually work for me very well: superuser.com/questions/262571/…Xanthic
@Virtuosity 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.Complicated
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 MyAwesomePasswordWellappointed
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.Fay
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:00000000Impassioned
@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.Vanden
@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.Thamos
@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. 😁Vanden
Why using this command with my administrator username it still says I am not authorized to modify files in "c:\program files" folder?Hileman
R
25

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
Rabblerouser answered 23/11, 2011 at 22:15 Comment(2)
I re-read your question and you don't want to be prompted. My suggestion will cause a password prompt. Sorry!Rabblerouser
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.Sines
I
9

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.

Interceptor answered 21/9, 2012 at 10:25 Comment(4)
Doesn't work here. Instead I just get the help output.Lamas
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"Commorancy
I don't even have this command in Windows 10. -- Not from the command prompt, or from Power Shell.Incest
@BrainSlugs83, you have to download it, the link is in the answer.Brandling

© 2022 - 2024 — McMap. All rights reserved.