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.
Advertisеment
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 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
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:
Credits 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!
Advertisеment
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
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.
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.
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.
Nice try, but it removes the “=” from any parameters… like
install -allusers=0 -silent=1
will result in
install -allusers 0 -silent 1
Otherwise, it runs fine, thanks!