How to code a BAT file to always run as admin mode?

WindowsBatch File

Windows Problem Overview


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

Windows Solutions


Solution 1 - Windows

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.

Solution 2 - Windows

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

runas /user:Administrator Example1Server.exe

Solution 3 - Windows

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.

Solution 4 - Windows

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

Solution 5 - Windows

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

Solution 6 - Windows

go get github.com/mattn/sudo

Then

sudo Example1Server.exe

Solution 7 - Windows

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

Solution 8 - Windows

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

Solution 9 - Windows

  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.

Solution 10 - Windows

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

Solution 11 - Windows

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 )

Solution 12 - Windows

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkarikariView Question on Stackoverflow
Solution 1 - WindowsEd GreavesView Answer on Stackoverflow
Solution 2 - WindowsKerrek SBView Answer on Stackoverflow
Solution 3 - WindowsSireView Answer on Stackoverflow
Solution 4 - WindowsaphoriaView Answer on Stackoverflow
Solution 5 - WindowsDheeraj BhaskarView Answer on Stackoverflow
Solution 6 - WindowsmattnView Answer on Stackoverflow
Solution 7 - WindowsxxedxxView Answer on Stackoverflow
Solution 8 - WindowsAnonymousView Answer on Stackoverflow
Solution 9 - WindowspstratonView Answer on Stackoverflow
Solution 10 - WindowsMartin VyskočilView Answer on Stackoverflow
Solution 11 - WindowsAchillesView Answer on Stackoverflow
Solution 12 - WindowsSamiamiasView Answer on Stackoverflow