44

How do I run a batch file from another batch file with administrator rights?

I have tried the RUNAS command, but it requires the administrator password.

I am searching for an alternative for running a batch file by right clicking on it and running as an administrator. I want to automate it from another batch file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vinay KV
  • 590
  • 1
  • 5
  • 10
  • user93353 gave you the answer, there is no else way. For security reason. Another option would be to give rights to your user to execute or write at the resource you are looking for. If you can, please give more details on want you want to achieve. – codea Sep 12 '13 at 04:58
  • possible duplicate of [Windows CMD Batch File Run A Command as Administrator](http://stackoverflow.com/questions/11414863/windows-cmd-batch-file-run-a-command-as-administrator) – Endoro Sep 12 '13 at 04:59
  • @Endoro - it's not a dup - he is asking how to do it non-interactively - the answer in the other does it interactively. – user93353 Sep 12 '13 at 05:11
  • Thanks for the reply guys. I used UAC control of windows to disable the authentication so that i can run Batch files from batch files. – Vinay KV Nov 17 '14 at 08:34
  • Does this answer your question? [How to code a BAT file to always run as admin mode?](https://stackoverflow.com/questions/6811372/how-to-code-a-bat-file-to-always-run-as-admin-mode) – Kaibo Nov 05 '19 at 08:57

9 Answers9

38

Put each line in cmd or all of theme in the batch file:

@echo off

if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)

"Put your command here"

it works fine for me.

MrSmile
  • 1,217
  • 2
  • 12
  • 20
  • caution: it works, but when running the batch-file then the working-directory of your commands will be `C:\WINDOWS\system32`. usually (without the elevation) the working-directory of your batch-file would be the folder where the batch-file is located in. – anion Nov 17 '20 at 08:44
26

On Windows 7:

  1. Create a shortcut to that batch file

  2. Right click on that shortcut file and choose Properties

  3. Click the Advanced button to find a checkbox for running as administrator

Check the screenshot below

Screenshot

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fedmich
  • 5,343
  • 3
  • 37
  • 52
  • 2
    Bad answer. OP asked for a way to run the batch file as admin from **within another batch file**, this doesn't work that way. –  May 02 '16 at 20:12
  • @prodigy, Have you tried and confirmed that it doesn't worked? – fedmich May 02 '16 at 23:04
  • 2
    I probably should've specified that I tried this on XP a while back, it didn't work. I haven't tried it recently so I may be wrong, or I may have done it incorrectly when I did. –  May 04 '16 at 19:01
15

You can use PowerShell to run b.bat as administrator from a.bat:

set mydir=%~dp0

Powershell -Command "& { Start-Process \"%mydir%b.bat\" -verb RunAs}"

It will prompt the user with a confirmation dialog. The user chooses YES, and then b.bat will be run as administrator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bismack163
  • 226
  • 2
  • 4
12

Use

runas /savecred /profile /user:Administrator whateveryouwanttorun.cmd

It will ask for the password the first time only. It will not ask for password again, unless the password is changed, etc.

user93353
  • 13,733
  • 8
  • 60
  • 122
  • 1
    That will allow the user to run arbitrary commands with administrative privileges, so you could just as well make him a member of the local administrators group. – Ansgar Wiechers Sep 12 '13 at 07:13
  • 2
    @AnsgarWiechers - in his own words, `I am searching for an alternative for running a batch file by right clicking on it and running as an administrator. i want to automate it from another batch file.` - if he is able to run it by right clicking and run as administrator, then he already knows the admin password - this is like sudo on linux. Another thing is that this cannot be used without administrator password. – user93353 Sep 12 '13 at 07:22
  • True, but irrelevant. `sudo` can be restricted to particular commands, `runas /savecred` can't. If you remove the password-prompt you effectively remove the barrier between admin and normal user. A better approach might be creating a scheduled task. – Ansgar Wiechers Sep 12 '13 at 08:15
  • @AnsgarWiechers - in this case, the user has access to root/admin password. In Linux, if he has root password, he can modify the sudoers file to remove any restriction on sudo. – user93353 Sep 12 '13 at 08:20
  • AFAIK, runas.exe doesn't provoke an elevation prompt, which appears to be what the OP is referring to. – Bill_Stewart Jul 30 '14 at 15:35
3

If you're trying to invoke a Windows UAC prompt (the one that puts the whole screen black and asks if you're granting administrator privileges to the following task), RUNAS is not the smoothest way to do it, since:

  1. You're not going to get prompted for UAC authorization, even if logged in as the administrator and
  2. RUNAS expects that you have the administrator password, even if your user is setup as a local administrator, in which case the former password is not a sound security practice, specially in work environments.

Instead, try to copy & paste the following code to ensure that your batch file runs with administrator privileges:

@echo off

>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (
    echo Requesting Admin access...
    goto goUAC )
    else goto goADMIN

:goUAC
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:goADMIN
    pushd "%CD%"
    CD /D "%~dp0"

rem --- FROM HERE PASTE YOUR ADMIN-ENABLED BATCH SCRIPT ---
echo Stopping some Microsoft Service...
net stop sqlserveragent
rem --- END OF BATCH ----

This solution works 100% under Windows 7, 8.1 and 10 setups with UAC enabled.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
alejandrob
  • 603
  • 8
  • 6
  • Sorry for the angry dude who down voted my answer. He clearly did not tried it, and to be fair I did answered the question that started this thread. This kind of ignorant negativity discourage contributors from wasting their time on people that do not try answers before voting down without evidence. – alejandrob Oct 02 '15 at 17:01
  • I think your problem was that you wrote *so* much code when there were much shorter and simpler solutions already posted. – MD XF Sep 30 '16 at 21:15
  • 1
    else goto goADMIN should be replaced with simply goto goADMIN – devzero May 11 '17 at 11:00
2

Runas.exe won't work here. You can use VBScript to invoke the "Run as Administrator" shell verb. The Elevation Powertoys contain a batchfile that allows you to invoke an elevated command:

elevatecmd.exe

http://blogs.technet.com/b/elevationpowertoys/

surfasb
  • 968
  • 1
  • 13
  • 31
2

CMD Itself does not have a function to run files as admin, but powershell does, and that powershell function can be exectuted through CMD with a certain command. Write it in command prompt to run the file you specified as admin.

powershell -command start-process -file yourfilename -verb runas

Hope it helped!

DCT A
  • 21
  • 5
0

The complete solution I found that worked was:

@echo off
cd /D "%~dp0"
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
"Put your command here"

credit for: https://stackoverflow.com/a/51472107/15087068

https://serverfault.com/a/95696

-1

This a trick that i used if anyone wants they can try this in batch file.This will give you the admin prompt when you run the batch file

@echo off
cd \ && cd windows/system32 && command which needs admin credentials
pause
Sushant Baweja
  • 202
  • 1
  • 5
  • 14