How to auto elevate a batch file to run it as administrator

Sometimes you need to create a batch file which includes commands that require elevated rights. Due to the UAC function included in Windows since Windows Vista, when start a batch file, it opens without the required access rights unless you right click it and select "Run as Administrator" from the context menu. Here is how you can create a batch file which will auto elevate itself.


Before you continue, see how to add Batch file (*.bat) to New menu of File Explorer. Place the following content at the beginning of your 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

Place your own batch commands which require elevation below the last line.

The provided code will create a special VBS file which will restart it if it is not running as Administrator. So, if you launch it with limited permissions, you will get a UAC prompt requesting you to elevate privileges before it runs its commands!

Here is how it looks in Windows 10:Windows 10 batch file

Windows 10 batch file UAC request

Windows 10 auto elevate a batch fileCredits for this code go to Matt.

Support us

Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:

If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!


Author: Sergey Tkachenko

Sergey Tkachenko is a software developer who started Winaero back in 2011. On this blog, Sergey is writing about everything connected to Microsoft, Windows and popular software. Follow him on Telegram, Twitter, and YouTube.

4 thoughts on “How to auto elevate a batch file to run it as administrator”

  1. working on RS1?
    cuz after all the cmd window just closing even if i put a pause after code
    and also tried to use simple text like in tutorial it closing anyway

    1. it working.
      screenshots are from rs1.
      execute your batch file from cmd.exe, not from explorer and see the output.
      it has some typo or some illegal character in the text.
      running it from cmd.exe will give you a hint.

      1. oh i see, but i can fix it for u if u wanna
        run bat from anywhere with admin rights
        and my code was much easier then that one.

  2. all you need to do is add this one line to the top of any batch file you want to elevate. copy and paste. anything after the line will run once you click Yes on the UAC prompt.

    >nul 2>&1 fsutil dirty query %systemdrive% || echo CreateObject^(“Shell.Application”^).ShellExecute “%~0”, “ELEVATED”, “”, “runas”, 1 > “%temp%\uac.vbs” && “%temp%\uac.vbs” && exit /b

    its way faster and more reliable than other methods. using fsutil dirty query works even in safe mode, where the net commands dont work if server services is not running.

Leave a Reply

Your email address will not be published.

Using Telegram? Subscribe to the blog channel!
Hello. Add your message here.