You can use my script Batch_Admin to automatically elevate the Administrator for use in any type of Batch own script, or BAT or .CMD. This function ensures the work on a standard environment, with standard tools and in the same way in different Windows systems. It uses several standard and built-in Windows commands. Do not use any other external scripts. It can be used to call any program as Administrator, not just scripts Batch. It suffices to create a small script, which then call the program that requires permission Administartor. Place this in any search PATH folder (from %PATH%).
To use it, you simply insert the following line (exactly as) in its parent Batch script, which is to be run as Administrator. This is best done at the top of our parent script and it is best that first, which will be called via the CALL further, some sub-scripts (see detailed explanation below).
net session >nul 2>nul&if errorlevel 1 Batch_Admin "%~0" %*
This can be inserted inside any child scripts, called by CALL syntax.
To display a message while it is still does not have Administrator privileges, eg. for 5 seconds before calling Batch_Admin set the variable "ShowAdminInfo" on the number of seconds
SET ShowAdminInfo=5
net session >nul 2>nul&if errorlevel 1 Batch_Admin "%~0" %*
So how it works:
• Calls UAC.ShellExecute through VBScript to pick up user permissions
• Batch runs the script, or BAT or CMD
• Script caller can have a long name spaces included in the call in quotes
• Give back all the parameters call the master script passed from the command line, as if there was no transmission of the call and start something from scratch, also with special characters, like exclamation marks (!) and percentage (%), and also with parentheses ()
• Holds a master script location if the call took place in the directory where this script is the master
• Auxiliary displays a message if we set before ourselves the variable "ShowAdminInfo", eg. for 5 seconds which can prevent the immediate passage of the rights of the Administrator giving additional information and the possibility of an informed decision. Examples of setting that must be set: SET ShowAdminInfo=5 By default, because this variable is not defined, it does not display.
• The script is calling also in the system search path % PATH% and from network resource, such as: \Computer\share\test.bat
• Prevents misunderstandings in writing the necessary auxiliary files when the script calls several Batch_Admin almost at the same time. It uses the random number from 1 to 100
• But, the best is to use it on parent script. The transition to Administrator rights always requires that you run the script completely again, in a new window, in a new environment, without recourse to the the variables are set previously in our script and values of these variables did not pass then to call as Administrator. Just the script will be executed again.
Below a complete script Batch_Admin. Extended information about this is on my site.
Copy the following text into Notepad and save it to disk as: Batch_Admin.bat
@echo off
if "%~1"=="" (echo *** Batch_Admin ***&echo.&echo Automatically get admin rights for another Batch. See info inside.&TIMEOUT /T 30>nul&goto:eof)
:: A D M I N I S T R A T O R - Automatically get admin rights for script batch. Paste this on top: net session >nul 2>nul&if errorlevel 1 Batch_Admin "%~0" %*
:: Also keep Batch directory localisation and then set variable: PATH_BAT
:: if earlier variable "ShowAdminInfo" is empty (not defined) then no info, else showing info with number of seconds
::
:: Elaboration: Artur Zgadzaj
setlocal
setlocal DisableDelayedExpansion
SET "Localy="
if exist "%~1" SET "Localy=YES"
if exist "%~1.BAT" SET "Localy=YES"
if exist "%~1.CMD" SET "Localy=YES"
if defined Localy FOR %%I IN ("%~1") DO SET "PATH_BAT=%%~dI%%~pI"
SET P1=%~1
SET Parameters=%*
SET Parameters=%Parameters:!=^^!%
setlocal EnableDelayedExpansion
SET Parameters=!Parameters:%P1%=!
SET Parameters=!Parameters:%%=%%%%!
setlocal DisableDelayedExpansion
SET Parameters=%Parameters:~3%
net session >nul 2>nul&if not errorlevel 1 goto Administrator_OK
if not defined ShowAdminInfo goto skip_message_Administrator
echo.
echo Script: %~1
echo.
echo *****************************************************************
echo *** R U N N I N G A S A D M I N I S T R A T O R ***
echo *****************************************************************
echo.
echo Call up just as the Administrator. You can make a shortcut to the script and set:
echo.
echo shortcut ^> Advanced ^> Running as Administrator
echo.
echo Alternatively run once "As Administrator"
echo or in the Schedule tasks with highest privileges
echo.
echo Cancel Ctrl-C or wait for launch %ShowAdminInfo% seconds ...
TIMEOUT /T %ShowAdminInfo% > nul
:skip_message_Administrator
MD %TEMP% 2> nul
SET /A $Admin$=%RANDOM% * 100 / 32768 + 1
SET "Percent="
del "%TEMP%\$Admin_%$Admin$%_Test.bat" 2>nul
echo %Parameters% > "%TEMP%\$Admin_%$Admin$%_Test.bat"
if not exist "%TEMP%\$Admin_%$Admin$%_Test.bat" SET Percent=^"
del "%TEMP%\$Admin_%$Admin$%_Test.bat" 2>nul
echo @SET "PATH_BAT=%PATH_BAT%" > "%TEMP%\$Admin_%$Admin$%_Batch_Start.bat"
echo @SET "BatchFullName=%BatchFullName%" >> "%TEMP%\$Admin_%$Admin$%_Batch_Start.bat"
if defined Localy (echo @CD /D "%PATH_BAT%" >> "%TEMP%\$Admin_%$Admin$%_Batch_Start.bat")
echo @"%~1" %Parameters% %Percent% >> "%TEMP%\$Admin_%$Admin$%_Batch_Start.bat"
echo SET UAC = CreateObject^("Shell.Application"^) > "%TEMP%\$Admin_%$Admin$%_Batch_getPrivileges.vbs"
echo UAC.ShellExecute "%TEMP%\$Admin_%$Admin$%_Batch_Start.bat", "", "", "runas", 1 >> "%TEMP%\$Admin_%$Admin$%_Batch_getPrivileges.vbs"
"%TEMP%\$Admin_%$Admin$%_Batch_getPrivileges.vbs"
endlocal
exit /B
:Administrator_OK
"%~1" %Parameters%
goto:eof
REM *** A D M I N I S T R A T O R - Automatically get admin rights (The End) ***
3
See also: How to request Administrator access inside a batch file
– ComFreek – 2014-07-28T14:53:15.797I was able to get this to work for me: http://stackoverflow.com/a/13811519/27928
– Rohit – 2014-10-17T18:26:19.2702how is two left clicks more automatic than one left- and one right click? – jiggunjer – 2016-01-17T12:44:33.193