Last active
May 31, 2024 17:18
-
-
Save Maximus5/a7fb0a11b3c33e5d96b0 to your computer and use it in GitHub Desktop.
Sample batch to run itself elevated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
echo Checking for permissions | |
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" | |
echo Permission check result: %errorlevel% | |
REM --> If error flag set, we do not have admin. | |
if '%errorlevel%' NEQ '0' ( | |
echo Requesting administrative privileges... | |
goto UACPrompt | |
) else ( goto gotAdmin ) | |
:UACPrompt | |
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" | |
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs" | |
echo Running created temporary "%temp%\getadmin.vbs" | |
timeout /T 5 | |
"%temp%\getadmin.vbs" | |
exit /B | |
:gotAdmin | |
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" ) | |
pushd "%CD%" | |
CD /D "%~dp0" | |
echo Batch was successfully started with admin privileges | |
echo . | |
cmd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another variant would be
It becomes more modern with the following code
edit: some bugs fixed