148

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

Community
  • 1
  • 1
Praveen Jakkaraju
  • 1,601
  • 4
  • 16
  • 13
  • 1
    possible duplicate of [Run Batch file as administrator - Windows 7 - Command "Run As" from network file system](http://stackoverflow.com/questions/10415653/run-batch-file-as-administrator-windows-7-command-run-as-from-network-file) – Adriano Repetti Oct 29 '12 at 15:50
  • 96
    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. – Steven Du Mar 18 '13 at 15:14
  • 2
    This was useful for me: http://superuser.com/questions/55809/how-to-run-program-from-command-line-with-elevated-rights Sometimes "run as administrator" is expected to mean "run with elevated rights". – User Jan 27 '14 at 23:14
  • `sudo.bat` might be useful to someone. http://pastebin.com/taHp4FzR – Niklas R Apr 23 '14 at 14:15
  • Read more for creating a vbscript to do that for you : http://ss64.com/vb/syntax-elevate.html – Future Mar 20 '16 at 22:50
  • 2
    Should be moved to superuser instead of closed (and also flagged as duplicate of http://superuser.com/questions/55809/how-to-run-program-from-command-line-with-elevated-rights ) – Julien Kronegg Apr 11 '16 at 05:55

3 Answers3

100

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dhana
  • 1,041
  • 1
  • 7
  • 3
  • 6
    While this may work correctly, it's an absolutely horrible idea as it undermines the whole concept of UAC. – Ben Voigt Dec 22 '15 at 15:08
  • 10
    @BenVoigt please suggest an alternative then – Daniel Sokolowski Feb 17 '16 at 06:56
  • 16
    Try `powershell -Command "Start-Process 'C:\program.exe' -Verb runAs"` (replace `C:\program.exe` by your command), see http://superuser.com/questions/55809/how-to-run-program-from-command-line-with-elevated-rights – Julien Kronegg Apr 11 '16 at 05:48
  • 14
    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 – RayLoveless May 11 '16 at 15:51
  • 4
    @BenVoigt Don't blow a gasket. Like any power command, it is both useful and necessary in specific contexts. – arkon Jul 19 '16 at 10:58
  • Here is what did eventually work for me very well: http://superuser.com/questions/262571/how-do-i-easily-elevate-when-running-a-jar-file – Dime Aug 10 '16 at 13:22
  • @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. – solstice333 Sep 30 '16 at 13:09
  • 2
    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` – Yash Kumar Verma Sep 16 '17 at 15:03
  • 3
    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. – undrline - Reinstate Monica Apr 17 '19 at 14:15
  • 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 – Giamma Theo Jul 05 '19 at 08:19
  • @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. – Jason C Jun 22 '21 at 01:55
  • 1
    @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](https://meta.stackexchange.com/a/332351/135695) When that attack happens, UAC is one of very few OS features left protecting you. – Ben Voigt Jun 22 '21 at 15:06
  • 1
    @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. – Jason C Jun 22 '21 at 15:57
  • Why using this command with my administrator username it still says I am not authorized to modify files in "c:\program files" folder? – Redoman Jan 08 '22 at 03:21
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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Ruiz
  • 2,371
  • 3
  • 20
  • 29
  • I re-read your question and you don't want to be prompted. My suggestion will cause a password prompt. Sorry! – John Ruiz Nov 23 '11 at 22:17
  • 15
    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. – Praveen Jakkaraju Nov 23 '11 at 22:42
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben Curthoys
  • 741
  • 7
  • 20