How to Automatically Elevate a Batch file to Run it as Administrator?

To elevate batch files manually, you would right-click on it and choose Run as Administrator. Here is a way to automatically elevate a batch file that requires elevated privileges to run correctly. This is equivalent to choosing “Run as Administrator” by right-clicking a batch file. In either case, the UAC prompt would still show up.

Automatically Elevating a Batch File

:: Automatically check & get admin rights V2
@echo off
ECHO =============================
ECHO Running Admin shell
ECHO =============================

setlocal DisableDelayedExpansion
set "batchPath=%~0"
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion

if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************

ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B

setlocal & pushd .
cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)

REM Run shell as admin (example) - put here code as you like
ECHO %batchName% Arguments: %1 %2 %3 %4 %5 %6 %7 %8 %9
cmd /k

Add your instructions to this .bat file under the “START” label.

This batch file creates a Vbscript file which then re-launches the batch file as administrator (if it’s not already running under administrator privileges) using the “runas” parameter which is needed to elevate it. The Vbscript & “runas” method has been covered in my old post Vbscripts and UAC elevation in Windows Vista and Higher.

Credits to Matt at Stack Overflow for this method. windows – How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?

Running Batch Files Elevated by Default

Besides the above automatic elevation method, from the client’s side you can tweak the registry so that batch files will always start elevated, showing the UAC prompt. This is done by changing the default double-click action from “open” to “runas” in the registry. Use these steps:

Start Regedit.exe and go to the following location:


Double-click (default) and set its value data as runas

bat file default runas elevated

Exit the Registry Editor.

After this change, batch files will always run elevated when double-clicked. Run as administrator would be the default option shown when you right-click a .bat file.

bat file default runas elevated

About the author

Ramesh Srinivasan founded back in 2005. He is passionate about Microsoft technologies and has a vast experience in the ITeS industry — delivering support for Microsoft's consumer products. He has been a Microsoft MVP [2003 to 2012] who contributes to various Windows support forums.

1 thought on “How to Automatically Elevate a Batch file to Run it as Administrator?

  1. Just one problem with your first idea – The batch file to run your second batch file elevated…. requires elevation. So youre just adding a step with the same result.

Leave a Comment