Skip to content

Instantly share code, notes, and snippets.

@mrpapercut
Created September 17, 2015 08:12
Show Gist options
  • Save mrpapercut/cb6486106f1486815b22 to your computer and use it in GitHub Desktop.
Save mrpapercut/cb6486106f1486815b22 to your computer and use it in GitHub Desktop.
Batch script that runs code as admin when called as non-admin
@ECHO OFF
GOTO checkAdmin
:: Check if batch-file is run with admin privileges
:checkAdmin
FSUTIL dirty query %systemdrive% >nul 2>&1
IF %errorLevel% == 1 (
GOTO isNotAdmin
) ELSE (
GOTO isAdmin
)
:isNotAdmin
:: If you want to run the code both as non-admin AND as admin, uncomment the following line:
:: CALL :codeToExecute
:: Create elevated process that runs this file again
SETLOCAL DisableDelayedExpansion
SET "batchPath=%~0"
SETLOCAL EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
EXIT /B
GOTO EOF
:isAdmin
CALL :codeToExecute
GOTO EOF
:codeToExecute
:: Here goes the code to execute
:EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment