188

I have this line inside my BAT file:

"Example1Server.exe"

I would like to execute this in Administrator mode. How to modify the bat code to run this as admin?

Is this correct? Do I need to put the quotes?

runas /user:Administrator invis.vbs Example1Server.exe
karikari
  • 6,627
  • 16
  • 64
  • 79
  • 1
    See also https://stackoverflow.com/questions/7690994/powershell-running-a-command-as-administrator – Ed Greaves Apr 07 '17 at 17:39
  • 1
    See also https://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator – Ed Greaves Apr 07 '17 at 17:39

12 Answers12

413

The other answer requires that you enter the Administrator account password. Also, running under an account in the Administrator Group is not the same as run as administrator see: UAC on Wikipedia

Windows 7 Instructions

In order to run as an Administrator, create a shortcut for the batch file.

  1. Right click the batch file and click copy
  2. Navigate to where you want the shortcut
  3. Right click the background of the directory
  4. Select Paste Shortcut

Then you can set the shortcut to run as administrator:

  1. Right click the shortcut
  2. Choose Properties
  3. In the Shortcut tab, click Advanced
  4. Select the checkbox "Run as administrator"
  5. Click OK, OK

Now when you double click the shortcut it will prompt you for UAC confirmation and then Run as administrator (which as I said above is different than running under an account in the Administrator Group)

Check the screenshot below

Screenshot

Note: When you do so to Run As Administrator, the current directory (path) will not be same as the bat file. This can cause some problems in many cases that the bat file refer to relative files beside it. For example, in my Windows 7 the cur dir will be SYSTEM32 instead of bat file location! To workaround it, you should use

cd "%~dp0"

or better

pushd "%~dp0"

to ensure cur dir is at the same path where the bat file is.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Ed Greaves
  • 4,807
  • 2
  • 22
  • 19
  • 13
    Ah, that was hidden indeed (most of us didn't think that that advanced button had that option in it). I'm almost sure whoever thought of the other complex answer didn't know that. This is the answer 90%+ of visitors will want. – j riv Feb 07 '13 at 09:51
  • 2
    I agree. this should be the answer. I also added a screenshot to make it easier to find the **advance** button – fedmich Feb 10 '15 at 14:20
  • 1
    This also applies to Windows Server 2012 R2. – garec Mar 23 '15 at 19:08
  • @LeiYang the shortcut settings like "run as admin" are stored in a .lnk file. – Ed Greaves Apr 07 '17 at 17:25
  • This somehow does not work on Windows 8.1 Enterprise – serup Aug 23 '17 at 09:57
  • 3
    This does not work for me on either Windows 10 or Windows 7. When I try to run as Administrator, either by right clicking the BAT file and "Run as Administrator", or using the technique described here the batch file flashes open for a second then closes immediately with no commands or programs in the batch file executing. I've tried running very simple batch files that just echo a "Hello World" and they too fail in this manner. This is very frustrating. I have, so far, not been able to find a solution. – Jonathan Elkins Sep 14 '17 at 21:06
  • I am trying what I described in my previous comment with an account that is a member of the domain's Administrators group. – Jonathan Elkins Sep 14 '17 at 21:13
  • 2
    @JonathanElkins did you try adding a pause at the end of the batch file? – Ed Greaves Sep 20 '17 at 20:58
  • In my case I couldn't get this to work if the bat file was on a network drive. Had to move it locally. – Sean Bannister Apr 06 '22 at 20:19
  • I wish there was a way to do this completely in a batch file without having the need to fiddle with the properties of a shortcut file. – Andreas May 18 '22 at 05:47
  • @Andreas you could try this: https://superuser.com/a/852877/16747 – Ed Greaves Jun 07 '22 at 15:27
81

Just add this to the top of your bat file:

set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )

It will elevate to admin and also stay in the correct directory. Tested on Windows 10.

Sire
  • 4,086
  • 4
  • 39
  • 74
  • 1
    Tested in Windows 7 and works great. But could you explain what it does? I'm not familiar with bat files. – Sunfloro Apr 05 '19 at 00:30
  • 3
    It creates a VBScript file with code that elevates you to admin (if you're not already), and runs the bat file again, this time as admin. – Sire Apr 25 '19 at 13:40
  • 1
    What I Exactly searching for ! no extra changes or extra file required! – MSS Jun 15 '19 at 05:12
  • 1
    what is getadmin.vbs ? – Kiquenet Jul 12 '19 at 16:01
  • @Kiquenet The temp vbs file that gets created – Sire Aug 26 '19 at 13:47
  • 8
    Adding your two lines of code to my existing batch file is a good solution for me. I had to also add `exit` to the end of my batch file or I would be left at the prompt. This is probably because the .vbs launches a second command prompt. – Ben Jan 16 '20 at 15:14
  • @Sire this script is perfectly worked! but 1 issue, how do i also add a checking if the bat file is already run as admin so that the code could skip this part. Appreciate your input on this! thanks! – Fariz Azmi Mar 29 '20 at 17:30
  • This is a pretty neat solution, especially if you want to share your `bat`s so that other people can use them. – Ayush Mandowara Nov 14 '20 at 17:54
  • 3
    This doesn't work if run from a drive other than the C drive. I get `The system cannot find the drive specified.` after elevating to admin. – mbomb007 Feb 19 '21 at 18:00
81

You use runas to launch a program as a specific user:

runas /user:Administrator Example1Server.exe
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 7
    @karikari: A name like this (`Example1Server.exe`) doesn't need to be put in quotes, but in some other cases (like spaces in the name: `Example1 Server.exe`) you would need them indeed. You *can* use the quotes even if they are not needed, though. – Andriy M Jul 25 '11 at 04:38
  • 21
    The admin account might not be named Administrator – Anders Jul 25 '11 at 15:56
  • 12
    @Anders: No, it might not; I'm assuming the OP can "spot the pattern". This wasn't a plz-give-me-teh-codez answer :-S By the way, I should add the OP might want some more elaborate command like `runas /User:abc "csript myscript.vbs"`, or `runas /User:abc "cmd /c start ..."`. – Kerrek SB Jul 25 '11 at 16:19
  • 19
    I fear this answer is not what 90% of visitors want, since it's even harder than right clicking->run as admin, even if it's technically correct. Check again the other answer. It's exactly what most will want. – j riv Feb 07 '13 at 09:47
  • 19
    No, this answer is technically incorrect. _Running as administrator_ is not the same as running under a user whose name happens to be Administrator, and the privileges are different. See the other answer. – Alexander Gelbukh Sep 07 '15 at 00:52
  • 1
    Note that this command requires you to manually enter the Administrator password when you run it. – simpleuser Mar 04 '19 at 16:37
  • I tried using these two lines. Works really well when we dont pass any parameters to the batch file in which I added these two lines. But when I executed the batch file froma cmd.exe window by passing arguments, I was getting the error (Expected End of Statement) - More here: https://stackoverflow.com/questions/62663219/800a0401-expected-end-of-statement-when-trying-to-run-with-parameters – Sum-Al Jul 01 '20 at 03:55
15

If you can use a third party utility, here is an elevate command line utility.

The source and binaries are available on GitHub.

This is the usage description:

Usage: Elevate [-?|-wait|-k] prog [args]
-?    - Shows this help
-wait - Waits until prog terminates
-k    - Starts the the %COMSPEC% environment variable value and
                executes prog in it (CMD.EXE, 4NT.EXE, etc.)
prog  - The program to execute
args  - Optional command line arguments to prog
aphoria
  • 19,796
  • 7
  • 64
  • 73
10

You can use nircmd.exe's elevate command

NirCmd Command Reference - elevate

elevate [Program] {Command-Line Parameters}

For Windows Vista/7/2008 only: Run a program with administrator rights. When the [Program] contains one or more space characters, you must put it in quotes.

Examples:

elevate notepad.exe 
elevate notepad.exe C:\Windows\System32\Drivers\etc\HOSTS 
elevate "c:\program files\my software\abc.exe"

PS: I use it on win 10 and it works

Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
6
  1. My experimenting indicates that the runas command must include the admin user's domain (at least it does in my organization's environmental setup):

    runas /user:AdminDomain\AdminUserName ExampleScript.bat
    

    If you don’t already know the admin user's domain, run an instance of Command Prompt as the admin user, and enter the following command:

    echo %userdomain%
    
  2. The answers provided by both Kerrek SB and Ed Greaves will execute the target file under the admin user but, if the file is a Command script (.bat file) or VB script (.vbs file) which attempts to operate on the normal-login user’s environment (such as changing registry entries), you may not get the desired results because the environment under which the script actually runs will be that of the admin user, not the normal-login user! For example, if the file is a script that operates on the registry’s HKEY_CURRENT_USER hive, the affected “current-user” will be the admin user, not the normal-login user.

pstraton
  • 1,080
  • 14
  • 9
5
go get github.com/mattn/sudo

Then

sudo Example1Server.exe
mattn
  • 7,571
  • 30
  • 54
5

When you use the /savecred argument, it asks for the password once, and than never asks for it again. Even if you put it onto another program, it will not ask for the password. Example for your question:

runas /user:Administrator /savecred Example1Server.exe

Anonymous
  • 738
  • 4
  • 14
  • 36
4

convert your batch file into .exe with this tool: http://www.battoexeconverter.com/ then you can run it as administrator

xxedxx
  • 65
  • 1
  • 1
  • 3
    Bear in mind that a lot of anti viruses are super sensitive on exe files that are built this way. "converting/encrypting" batch files this way, is an old way used by malicious programs. – Hamy May 29 '16 at 06:13
  • If you analyze this domain using Symantec -- Threat Type: othermalware Threat Reason: Domain reported and verified as serving malware. Identified as malicious domain or URL. – Sunil Feb 19 '18 at 02:07
  • Bad suggestion! – Vlad Feb 19 '20 at 13:10
4

I Tested @Sire's answer on Windows 11, and it works like a charm. It's worth mentioning that using cmd /k - as @Sire has used - will keep the Administrator CMD open after it finishes running. Using cmd /c instead will close the window when it's over with the batch file.

set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/c cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )
Achilles
  • 1,554
  • 1
  • 28
  • 36
  • 2
    Works perfectly also on Windows 10 as of the latest update (March 2022). In contrast, Sire's answer did NOT actually work. – P. Shark Mar 23 '22 at 11:39
1

I found there is possible to use powershell. The powershell will show the default Windows UAC Dialog.

powershell Start -File Example1Server.exe -Verb RunAs

For execute BAT file with admin rights, the content of the BAT file can look as this:

@echo off
if "%1"=="runas" (
  cd %~dp0
  echo Hello from admin mode
  pause
) else (
  powershell Start -File "cmd '/K %~f0 runas'" -Verb RunAs
)

where:

  • %1 First input argument assigned to BAT file.
  • %~f0 expands to full path to the executed BAT file
  • %~dp0 expands to full directory path from where the BAT file is executed
  • cmd -C <commands> Execute command in terminal and close
0

Use the complete physical drive\path to your Target batch file in the shortcut Properties.

This does not work in Windows 10 if you use subst drives like I tried to do at first...