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

One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support. It won't take more than 10 seconds of your time. The share buttons are right below. :)

Ramesh Srinivasan is passionate about Microsoft technologies and he has been a consecutive ten-time recipient of the Microsoft Most Valuable Professional award in the Windows Shell/Desktop Experience category, from 2003 to 2012. He loves to troubleshoot and write about Windows. Ramesh founded in 2005.

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

    • It didn’t for me in Win10. In fact this is the first solution Ive found that works. Only issue is there is random,extra shell window open with a path to the batch file i ran. But thats no biggie.

Leave a Comment