An auto elevated batch

At times, we need to run some batch files which require administrator privilege. In order to do this, we have to right click the file, then click run as admin. Errors will be shown if we run the batch file directly without elevated privilege. Is there a way to make the batch file elevate itself, if it doesn't have admin privilege?

How to elevate automatically

There's a pretty good discussion on stackoverflow, with several solutions provided. Briefly, the basic flow is like this:

  1. If we got admin privilege, skip Step 2 if yes
  2. Create a temp script file or object with elevated privilege and re-run the batch
  3. Execute batch operation which need admin privilege

Choosing solution

I think this answer is the most simple and neat, with the following reasons:

  • Cmd is the only thing needed, while some solutions might require powershell or other dependency
  • No temp files, while some solutions might create a temp file to get elevation
  • Simple to understand the elevating flow, but may not support passing arguments on the other side

Batch Code

I made some modification to the original code, according to the other answers, final code and comment is posted here:

@ECHO OFF
:: the first line is to turn off output
:: check if we have admin privilege, do not output result, nor errors
NET SESSION 1>NUL 2>NUL
:: if not, jump to ELEVATE block
IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
:: jump to the batch script which needs admin privilege
GOTO ADMINTASKS

:: ELEVATE block
:ELEVATE
:: record current path
CD /d %~dp0
:: re-run this batch with admin privilege, using javascript
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1);close();"
:: exit
EXIT

:: our actual batch
:ADMINTASKS
:: add custom content below

we could remove comment to make it short:

@ECHO OFF
NET SESSION 1>NUL 2>NUL
IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
GOTO ADMINTASKS

:ELEVATE
CD /d %~dp0
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1);close();"
EXIT

:ADMINTASKS

Test

  1. let's add a test.cmd to our desktop, with the following content:

    @ECHO OFF
    NET SESSION 1>NUL 2>NUL
    IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
    GOTO ADMINTASKS
    
    :ELEVATE
    CD /d %~dp0
    MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1);close();"
    EXIT
    
    :ADMINTASKS
    net user
    pause
  2. Double click the file to run the batch, we'll see the UAC window which ask us if we're going to run the batch with admin privilege, select Yes https://i.loli.net/2018/05/25/5b0769b1eca8f.png
  3. We could check if our actual admin task is executed correctly, which will show users on our machine and pause in our test batch https://i.loli.net/2018/05/25/5b076affcc985.png

Summary

Finally, we could forget to run our admin batch with right click, which needs a little modification