Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am wondering whether it is possible to automatically open a batch file as admin when you double click on the batch file, because the commands need to be run with administrative rights.

Note: I already know about solution that require right clicking on the file and clicking on Run As Administrator, but this is not automatical solution.

share|improve this question
1  
    
I was able to get this to work for me: stackoverflow.com/a/13811519/27928 –  Rohit Oct 17 at 18:26

5 Answers 5

up vote 10 down vote accepted

Yes, you're able to run a batch file with administrative rights. Unfortunately, you can't do this directly from the batch file it self. You would need to first create a shortcut of that batch file and change the properties for that shortcut in order to make this work.

To create a shortcut, there are many ways but the simplest and the fastest way is by using the Send to option in the context menu.

Right click batch file > Send to > Desktop (create shortcut)

Of course you can send the shortcut to where ever you would like. To elevate the batch file to run as admin, follow the steps below:

  1. Right-click the shortcut you just created (should be on the desktop or where ever you send it)
  2. Under the Shortcut tab, click the Advanced... button.
  3. Check the Run as administrator checkbox and press OK to both the modal window and the main properties window.
  4. Run the shortcut by double-clicking it and the batch file should run as administrator.
share|improve this answer
    
Thanks this really helped alot! –  Antp Jul 28 at 4:50
1  
You could always force the batch file to call the shortcut, and have the shortcut pass a parameter to know not to call the short cut. You would insert something like if "%~1" == "" start %USERPROFILE%\Desktop\Shortcut.lnk && GOTO :EOF right below @echo off, and make the shortcut link to program.bat a instead of just program.bat. –  trlkly Jul 28 at 5:20

Alternatively you could create a second batch file, this could allow you to run anything as an administrator, while only entering the administrator password once. The password is saved in an authentication token, and takes some real computer know how to crack.

runas /savecred /user:(domain)\(admin user) "(file path to exe,bat,whatever)"
share|improve this answer

You can run batch files as admin by right clicking on the file and clicking on Run As Administrator

Alternatively, you could also do Shift+Right Click then click on Run as Different User if you need to run it as a user with administrative privileges.

share|improve this answer
3  
Sorry this is useless because i want it to run as admin automatically. –  Antp Jul 28 at 4:52
    
Right. Misread the question. –  Lawrence Jul 28 at 4:53

The accepted answer does indeed work. But I found that in Windows 7, I had to endure the UAC dialog each time the shortcut is clicked. This IMHO detracts significantly from the "automatically" in this question!

Then I found that in my own situation, the .bat file in question is to be run by Task Scheduler. In this case, checking the Run with highest privileges option on the General tab of the task, nicely takes care of the problem. The .bat is then run as administrator without any hassles.

PS: I didn't realize I couldn't upload images on this answer, 'cause I have a nice little screenshot sitting right with me now! Or can I?

share|improve this answer
2  
If you add links to the images somebody will inline them for you. –  Ramhound Oct 7 at 18:50
    
@Ramhound. Add links to the images? The image is on my desktop, not somewhere online. Or, surely I'm missing something? –  Ifedi Okonkwo Oct 8 at 18:10
    
Yes; You upload them; You just can't inline the screenshots into your answer. –  Ramhound Oct 8 at 18:19
    
Either that upload button wasn't there, or I was half-blind yesterday! I'm sure it was the latter. Anyhow, I completed the file upload very nicely, only to be told I needed a rep of 10 to post images. Thanks, though. –  Ifedi Okonkwo Oct 8 at 18:27
    
You can still place the link in the answer though. –  Ramhound Oct 8 at 18:29

As posted in an answer by Ben Gripka to another question:

REM --add the following to the top of your bat file--


@echo off

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    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

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------
share|improve this answer
    
Yes i found this and am using it currnetly. Thanks anyways! –  Antp 7 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.