Skip to content

Instantly share code, notes, and snippets.

@Maximus5
Last active March 18, 2024 17:28
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Maximus5/a7fb0a11b3c33e5d96b0 to your computer and use it in GitHub Desktop.
Save Maximus5/a7fb0a11b3c33e5d96b0 to your computer and use it in GitHub Desktop.
Sample batch to run itself elevated
@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
@AhmedNSane
Copy link

Great job! Is it possible to make this run as the SYSTEM user instead?

@Maximus5
Copy link
Author

Sure, it's possible. But much more complicated.
You have to run it elevated first, and after that start the script via task scheduler under system account.
Too much work I think.

In ConEmu you can do that easy BTW

@MiggieNRG
Copy link

Not working for me. It keeps looping and restarts after 5 sec are over been doing that now for 15 mins ))))): help

@AhmedNSane
Copy link

@Maximus5 Don't remember exactly why I asked you that. 😅 Maybe I needed to change some stubborn file permissions or something, and I ended up settling for nSudo. I know it's late, but I appreciate YOUR quick response. 🙏🏻

@AhmedNSane
Copy link

@MiggieNRG Maybe try turning the User Account Control notifications off, and see if it'll make a difference (just for debugging purposes, since I don't see why it wouldn't work otherwise.) I use the script every time I feel too lazy to right-click a file, and click "Run as administrator", and so far, it has never failed me. Hope this helps, Miguel.

@Maximus5
Copy link
Author

Maximus5 commented Jan 2, 2022

General debugging tips:
Remove "echo off"
Add pause command in some places
Run and see the output

@LizenzFass78851
Copy link

LizenzFass78851 commented Mar 2, 2024

Another variant would be:

@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
if exist "%SYSTEMROOT%\System32\Cscript.exe" ( goto VBS )
goto PS

:VBS
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

:PS
REM timeout /T 5
powershell -Command "Start-Process -Verb RunAs -FilePath '%0' -ArgumentList 'am_admin'"
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

This primarily uses the execution in vbs. If removed from newer Windows versions, execution in powershell is used


It becomes more modern with the following code

@echo off

>nul 2>&1 fsutil dirty query %systemdrive% && (goto gotAdmin) || (goto UACPrompt)
:UACPrompt
if exist "%SYSTEMROOT%\System32\Cscript.exe" (
    echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "%~s0", "", "", "runas", 1 > "%temp%\getadmin.vbs" 
    "%temp%\getadmin.vbs"
    exit /b
) else (
    powershell -Command "Start-Process -Verb RunAs -FilePath '%0' -ArgumentList 'am_admin'"
    exit /b
)
:gotAdmin
if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs"
pushd "%CD%" && CD /D "%~dp0"
cls
cmd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment