When running batch files under Win10 system, you will find that many batch files will prompt that administrator rights are required when running. At this time, you have to manually set it to run as an administrator to operate. Such semi-automated operations are really annoying. Wouldn't it be much faster if we could automatically add administrator rights to batch files? Because in this case, we can directly operate by double-clicking to run. So, how to make the batch file automatically add administrator rights under Win10 system? Today, the editor of Huajun brings you a simple and easy-to-understand solution. I hope you will support and share the carefully selected content. If you like it, hurry up and get it!
The steps are as follows:
It is not difficult to automatically add administrator permissions to a batch file. As long as you use the code provided by the editor, you can easily achieve the function of automatically obtaining permissions. Copy the following code into Notepad and save it in .bat format (the file name can be customized);


- 01::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::02::SoftMedia Rubik's Cube automatically adds administrator rights to batch files::03::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::04@echo off05CLS06ECHO.07ECHO ================================08ECHO Soft Media Cube obtains batch file administrator rights09ECHO ================================10:init11setlocal DisableDelayedExpansion12set "batchPath=%~0"13for %%k in (%0) do set batchName=%%~nk14set "vbsGetPrivileges=%temp%OEgetPriv_%batchName%.vbs"15setlocal EnableDelayedExpansion16:checkPrivileges17NET FILE 1>NUL 2>NUL18if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )19:getPrivileges20if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)21ECHO.22ECHO *********************************23ECHO requesting UAC permission approval...24ECHO *********************************25ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"26ECHO args = "ELEV " >> "%vbsGetPrivileges%"27ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"28ECHO args = args ^& strArg ^& " " >> "%vbsGetPrivileges%"29ECHO Next >> "%vbsGetPrivileges%"30ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"31"%SystemRoot%System32WScript.exe" "%vbsGetPrivileges%" %*32exit /B33:gotPrivileges34setlocal & pushd.35cd /d %~dp036if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1)37:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::38::The following is the batch file code that needs to be run::39:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::40rem The following line can be modified to the bat command you need (you can delete the colons in the three lines above to the bottom)41ECHO Welcome to the Soft Magic Cube!42ECHO.43pause
复制代码::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 软媒魔方自动添加批处理文件管理员权限::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::@echo offCLSECHO.ECHO ========================= "vbsGetPrivileges=%temp%OEgetPriv_%batchName%.vbs"setlocal EnableDelayedExpansion:checkPrivilegesNET FILE 1>NUL 2>NULif '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ):getPrivilegesif '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)ECHO.ECHO ********************************ECHO Request UAC permission approval...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%System32WScript.exe" "%vbsGetPrivileges%" %*exit /B:gotPrivilegessetlocal & pushd .cd /d %~dp0if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1)::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: The following is the batch file code that needs to be run ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::Rem The following lines of this line can be modified to the bat command you need (you can delete the colons in the three lines above) ECHO Welcome to the Soft Magic Cube! ECHO.pause