How self raise a batch file to run as administrator

12.23
How self raise a batch file to run as administrator -

Sometimes you need to create a batch file that includes commands that require elevated privileges. With UAC included in Windows since Windows Vista, when to start a batch file, it opens without the required access rights, unless you right-click and select "Run as administrator" from the context menu . Here's how you can create a batch file that will automatically rise.

Before continuing, see how to add batch file (* .bat) to New File Explorer menu. Place the following content at the beginning of your batch file:

 :::::::::::::::::::::::::::::::: :::::::::::: :: V2 automatically check and get admin rights :::::::::::::::::::::::::::: : ::::::::::::::: @echo off CLS ECHO. ECHO ECHO ============================= shell administration Execution ECHO ============= == ==============: init setlocal set DisableDelayedExpansion "batchPath% = ~ 0" %% k (0%) not set BatchName = %% ~ nk set "% temp = vbsGetPrivileges % % OEgetPriv_ BatchName% .vbs "setlocal ENABLEDELAYEDEXPANSION: checkPrivileges NET FILE 1> NUL 2> NUL if '% errorlevel% ==' 0 '(goto gotPrivileges) other (of getPrivileges goto): getPrivileges if"% 1 "= = 'ELEV' (echo ELEV & drop / 1 & goto gotPrivileges) ECHO. ECHO ECHO ************************************** Invoking UAC Privilege Escalation ECHO **** Set UAC ********************************** ECHO ^ = CreateObject ( "Shell.Application" ^)> " %% vbsGetPrivileges "ECHO args =" ELEV ">>"% vbsGetPrivileges% "ECHO each strArg in WScript.Arguments >>"%% vbsGetPrivileges "args = args ECHO ^ & ^ & 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 & drop / 1) :::::::::::::::: :::::::::::: :: BEGIN :::::::::::::::::::::::::::: REM Run as administrator shell (for example) - put here the code you like ECHO% BatchName% Arguments:% 1% 2% 3% 4% 5% 6% 7% 8% 9 

Place your own commands lots require elevation below the last line.

The provided code will create a special file that VBS will restart if it does not work as administrator. So if you run with limited permissions, you get a UAC prompt asking you to elevate privileges before executing its orders

This is what it looks like in Windows 10: Windows 10 batch file

Windows 10 batch file UAC request

Windows 10 auto elevate a batch file this code Credits go to Matt .

Previous
Next Post »
0 Komentar