Scripting Question

Batch run as administrator - automatically with highest privileges

05/31/2015 1449 views

Batch_Admin I present a script 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.

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" %*



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 from a group administrator to full Administrator privileges
   Of course, for the ordinary user, outside the group Administrators does not apply, at most, the system will ask for a change of user

- Batch runs the script, or BAT or CMD with elevated Administrator rights, but only if it is not already as Administartor.
   If a script is already running with Administrator rights, it does nothing, not even invoked. When you set earlier variable
   ShowAdminInfo on the number of seconds it even tells you how to call himself the master script as Administartor.



Terms of action:

- Tool Batch_Admin.bat best placed somewhere in the system directory searchable% PATH%, eg. in   C:\Windows\System32
   You can also put it in the directory where we have our parent script, which we then refer to Batch_Admin.bat,
   But then we must remember that the call to our script from another directory will not use Batch_Admin if it did not we ask
   the transition to this directory.
It is best to put it in a directory PATH



How does run with elevated Administrator rights when Batch_Admin diagnose, it is necessary:

- The parent script may be calling with the extension .BAT or .CMD type

- 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, although this is:
   * Also includes various special characters, eg. good moves exclamation marks (!) And percentage (%)
   * Parameters include the use of parentheses  () and passes them correctly back
     Therefore, that they were not misinterpreted, in Batch_Admin are used sometimes jumping like: goto
   * Takes into account variables with long names inside quotation marks, with spaces between
   * Including even a single quote, or rather an odd number of the quotes. He is then added at the end
     the additional space to a pair of quotation marks, but at least the script calls the parent of the other parameters
   * After calling the script parent it is the same calling parameters, that is, for the %0 %1 %2, and so on
     as well as if someone has studied all the parameters at once as: %*

- 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 also in the system search path % PATH%

- Even more, because you can invoke the script with network path, an unmapped network resource, such as .:
  Then the script also "test.bat" will be run as Administrator, provided, however, that locally
  the PATH somewhere we have placed Batch_Admin. See comment above in the "Operating Conditions"

- 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

- Sets one additional variable for use later in the script that causes it when it is in the current directory:
  See also note below.
        PATH_BAT - the same path to the location of the script, eg .:   C:\UTILS
  NOTE: If, however, the slave Batch script caused function CALL from one another need to have the correct value of these
         variables, you have yourself to add two lines (necessary when switching to the Administrator took place in the parent
         script, because then including a child, as we mentioned above, our Batch_Admin no longer cause and not set properly
         these variables possibly be old, appropriate for the parent script, and not the slave) - the third row is
         optional for the transition to this directory and can be combined mark & ​​at the end of the second line:


               FOR %%I IN ("%~0") DO SET "PATH_BAT=%%~dI%%~pI"

               SET "BatchFullName=%PATH_BAT%%~0"


               CD /D "%PATH_BAT%"



General principles and calling up sub-CALL command Batch Scripting

  NOTE: 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.
         You can set a couple of variables previously SET command, and then trigger a transition to
         Administrator, but it will be the illusion that it remembers. Simply set these variables over again,
         again, only the second time that no longer need to call again as Administrator
         and go further. Hence arises the illusion that remembers the first set, and he charges them
         again, and the original call does not come back, leave them permanently.

   So, if you call multiple nested scripts, one second of return, a means by CALL
   it actually should be called Batch_Admin at first, overriding the script, which will then
   call another one. There is no mistake paste the calling line Batch_Admin in each successive,
   sub-script, because they do in fact Batch_Admin will not be called, or
   there will be nothing to do.

   And call Batch_Admin at first, overriding our script is of crucial importance
   for proper return to the calling script host and all subsequent and remembrance
   pre-set variables, and the transmission of the variables from the slave scripts.
   Setting call Batch_Admin in subsequent sub-script is no longer relevant.

   Let us therefore specific situation, when bad work and the situation where the well work, after repair
     Let's start with a bad call. In the example shown here in a second script, "2.bat" get the message:

            [2] Variable_1 - I know nothing of this variable

     while the first script 1.bat will not know anything about setting up the second for Variable_2, namely:

            [1] Variable_2 - I know nothing of this variable

     Repair:     Just first script run as Administrator alone or also in him, that is, in "1.bat"
                      insert at the top of the call Batch_Admin as:
     net session >nul 2>nul&if errorlevel 1  Batch_Admin "%~0" %*


     The parent script, let's call him 1.BAT


     @echo off

     echo [1] I'm beginning ...


     SET Variable_1=abc


     CALL 2.BAT


     if     defined Variable_1   echo [1] Variable_1 - Yes, I know this variable

     if not defined Variable_1   echo [1] Variable_1 - I know nothing of this variable

     if     defined Variable_2   echo [1] Variable_2 - Yes, I know this variable

     if not defined Variable_2   echo [1] Variable_2 - I know nothing of this variable




    The child script, let's call him 2.BAT


     @echo off

     REM *** A D M I N I S T R A T O R  ***

     net session >nul 2>nul&if errorlevel 1  Batch_Admin "%~0" %*


     SET Variable_2=xyz


     if     defined Variable_1   echo [2] Variable_1 - Yes, I know this variable

     if not defined Variable_1   echo [2] Variable_1 - I know nothing of this variable

     if     defined Variable_2   echo [2] Variable_2 - Yes, I know this variable

     if not defined Variable_2   echo [2] Variable_2 - I know nothing of this variable


Below is the complete script Batch_Admin
Copy the following text into Notepad and save it to a disk as:   



@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    Addition informations:  www.widzenia.com/info   (login: info  password: computer)


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 Script:  %~1


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 Call up just as the Administrator. You can make a shortcut to the script and set:


echo          shortcut ^> Advanced ^> Running as Administrator


echo     Alternatively run once "As Administrator"

echo     or in the Schedule tasks with highest privileges


echo Cancel Ctrl-C or wait for launch  %ShowAdminInfo%  seconds ...

TIMEOUT /T %ShowAdminInfo% > nul



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"



exit /B



"%~1" %Parameters%


REM *** A D M I N I S T R A T O R  - Automatically get admin rights  (The End)  ***

Artur Zgadzaj      other tools:  www.widzenia.com/info    (login: info   password: computer)

3 Comments   [ + ] Show comments


  • So this script does the same as me clicking "Run as Administrator" manually?
  • Exactly, but ...

    1. You can manually so the circle run their scripts as Administrator
    2. You could always remember to set the launcher to his calling Batch script as Administrator

    but ...

    How much work effectively as an administrator it is frustrating, is not it?
  • This looks pretty interesting, I will have to try it out. I would recommend in the future that you put information like this into a blog post. I think more people would see it that way: http://www.itninja.com/blog/post/new
    • Thank You, I post it at: http://www.itninja.com/blog/view/batch-run-as-administrator-automatically-with-highest-privileges-by-tools-batch-admin

Be the first to answer this question

This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ