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
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:init
setlocal DisableDelayedExpansion
set "batchPath=%~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%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B

:gotPrivileges
setlocal & pushd .
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
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:

HKEY_CLASSES_ROOT\batfile\shell

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

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.

4 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

Exit mobile version
1 Shares
Tweet
Share1
Share