14

How do I start/call a batch file from another, but with administrative privileges, so that it doesn't give me errors like the following?

Access is denied error code 5

Here is something like what I would like it to be.

echo PLEASE TYPE YOUR USERNAME AND PASSWORD IN THE FIELDS BELOW.
echo.
echo.
echo.
echo.
set /p u=Username:
echo.
set /p p=Password:
start next.bat %u% %p%
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cmd
  • 563
  • 3
  • 10
  • 26
  • Use the `runas` command. –  Aug 04 '13 at 16:56
  • @a_horse_with_no_name could you please elaborate, I did `runas /?` and the syntax doesn't show how to supply a password without being prompted. – cmd Aug 04 '13 at 17:18
  • possible duplicate of [Batch Script to Run as Administrator](http://stackoverflow.com/questions/14639743/batch-script-to-run-as-administrator) – Endoro Aug 04 '13 at 17:20
  • and much more with Google: http://www.verboon.info/index.php/2011/03/running-an-application-as-administrator-or-in-compatibility-mode/ http://stackoverflow.com/questions/12913652/batch-file-running-as-administrator – Endoro Aug 04 '13 at 17:24
  • @endoro I don't plagiarize thank you. – cmd Aug 04 '13 at 17:36

7 Answers7

4

No matter which way you chose, You must accept run it with admin privilege, so the point is which way is shorter? You could Right Click > Run as administrator as jean-Michael said although I prefer james approach (using vbScript) but if you don't want to use another vbScript file and just want benefit of just click on batch file and accept run with admin privilege (Note you have one right click and left click less) I suggest you this:

create a shortcut from your batch file and right click on shortcut > Properties > Shortcut Tab > Advanced now check the Run as administrator check-box. every time you execute it from the shortcut you just have one click to accept run it with admin privilege.

Hope this help.

QMaster
  • 3,743
  • 3
  • 43
  • 56
3

Sometimes third party utilities like AutoIt (see runas function) are not an option - but if you do have that option, check it out as that will let you do exactly what you're aiming to. You can then call the AutoIt script from your script and use its runas function.

Windows runas doesn't support providing a password unless you're happy with the /savecred option - which is fine if you're only running the task from a single computer. The first time it will ask you for a password, but after that it won't (though you still have to use /savecred option each time you use it). I've got a feeling using this could be a huge security hole. But since it seems this is for your own machine, in your batch use this:

runas /user:computername\username /savecred yourcommand.exe

Another way is to make a scheduled task that can be called by your script. You can make it using the GUI or from an elevated command line as described here.

You can then call it from your script like this:

SCHTASKS /Run /TN yourtaskname
Community
  • 1
  • 1
fzzylogic
  • 2,183
  • 1
  • 19
  • 25
2

Simply put the bat file into the Windows directory, and it will run as administrator. I tried this myself, and it worked:

C:\Windows\batch_file.bat

It should work like that.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tony TCG
  • 313
  • 3
  • 8
2

Within the batch file itself there is no way to run as an administrator, however if you launch the batch file from within a .VBS file, you are able to specify a 'runas' parameter.

set shell=CreateObject("Shell.Application")
shell.ShellExecute "your_batch_file.bat",,"C:\path\to\thedirectory", "runas", 0 
set shell=nothing 

This will launch your batch file as an administrator, and you can enable or disable the shell display (this example hides it as i wanted my program to run in the background without being seen).

James
  • 21
  • 1
1

Right click -> Run as administrator.

I think microsoft made as much as they could to prevent batch script to get administrative privileges on their own.

Jean-Michaël Celerier
  • 7,412
  • 3
  • 54
  • 75
  • 1
    @jean-Michael_Celerier thank you but I would like to do this within the batch itself. – cmd Aug 04 '13 at 17:05
  • 1
    @cmd do you see the security flaw it could be ? anybody could make a random batch script and format c:... – Jean-Michaël Celerier Aug 04 '13 at 17:19
  • 1
    not really because they'd still have to supply the password. when you run as admin manually, or pin it to your taskbar and then edit it to always run as admin (like my cmd.exe) then you don't need admin pass, you just either click the yes button, or after changing settings simply click the icon. @jean-Michael Celerier – cmd Aug 04 '13 at 17:40
1

@cmd, I posted an example (How can I test effective permissions of a user from a batch script?) to run another bat file with ShellExecute and elevated rights (only when it's needed).

Take a look if it's what you looking for and what you need. If not, let me know and we could adapt your script to make it work.

good luck

Community
  • 1
  • 1
gmo
  • 8,860
  • 3
  • 40
  • 51
1

I actually joined just to answer this, the simplest way by far is to create a shortcut to the program you want to run, then set the shortcut to run as administrator and just call the shortcut from the batch file. This will run with the settings specified in the shortcut and you could place this shortcut in the same folder as your batch file or just call it from the start menu.

Example: "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Folder\Shortcut Name.lnk"