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

::::::::::::::::::::::::::::::::::::::::::::
:: Elevate.cmd - Version 45
:: Automatically check & get admin rights
:: see "https://stackoverflow.com/a/12264592/1016343" for description
::::::::::::::::::::::::::::::::::::::::::::
 @echo off
 CLS
 ECHO.
 ECHO =============================
 ECHO Running Admin shell
 ECHO =============================

:init
 setlocal DisableDelayedExpansion
 set cmdInvoke=1
 set winSysFolder=System32
 set "batchPath=%~dpnx0"
 rem this works also from cmd shell, other than %~0
 for %%k in (%0) do set batchName=%%~nk
 set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
 setlocal EnableDelayedExpansion

:checkPrivileges
  NET FILE 1>NUL 2>NUL
  if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
  if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
  ECHO.
  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%"
  
  if '%cmdInvoke%'=='1' goto InvokeCmd 

  ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
  goto ExecElevation

:InvokeCmd
  ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
  ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"

:ExecElevation
 "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
 exit /B

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

 ::::::::::::::::::::::::::::
 ::START
 ::::::::::::::::::::::::::::
 REM Run shell as admin (example) - put here code as you like

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:

HKEY_CLASSES_ROOT\batfile\shell

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

    Reply
    • 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