cmd
cmd

Reputation: 593

How to start a program with admin privileges in a batch file

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%

Upvotes: 17

Views: 99980

Answers (7)

ThatGothGuy
ThatGothGuy

Reputation: 21

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"

Upvotes: 2

QMaster
QMaster

Reputation: 3904

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.

Upvotes: 4

James
James

Reputation: 21

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

Upvotes: 2

Tony TCG
Tony TCG

Reputation: 323

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.

Upvotes: 2

fzzylogic
fzzylogic

Reputation: 2283

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

Upvotes: 3

gmo
gmo

Reputation: 9010

@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

Upvotes: 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.

Upvotes: 0

Related Questions