Skip to content

Instantly share code, notes, and snippets.

@Maximus5
Last active November 29, 2025 12:31
Show Gist options
  • Select an option

  • Save Maximus5/a7fb0a11b3c33e5d96b0 to your computer and use it in GitHub Desktop.

Select an option

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
@TheNiceSky
Copy link

not works any of on windows 11

@LizenzFass78851
Copy link

My previous example did not work under Windows 11 when vbscripts.dll was removed.

Therefore, here is the corrected example, which works under Windows 11 with vbsscripts.dll removed.:

@echo off

>nul 2>&1 reg.exe query HKU\S-1-5-19 && (goto gotAdmin) || (goto UACPrompt)
:UACPrompt
if exist "%SYSTEMROOT%\System32\Cscript.exe" (
    if exist "%SYSTEMROOT%\System32\vbscript.dll" (
        echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "%~s0", "", "", "runas", 1 > "%temp%\getadmin.vbs"
        cscript //nologo "%temp%\getadmin.vbs"
        exit /b
    )
)
>nul 2>&1 where /Q powershell.exe && (
    powershell -Command "Start-Process -Verb RunAs -FilePath '%~s0'"
    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