Last active
August 21, 2024 03:06
-
-
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 |
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
It becomes more modern with the following code
edit: some bugs fixed
@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"
cscript //nologo "%temp%\getadmin.vbs"
exit /b
) else (
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
Another variant would be
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 edit: some bugs fixed@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" cscript //nologo "%temp%\getadmin.vbs" exit /b ) else ( 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
Based on your code, I have added support for Windows Terminal and gsudo.
@echo off
>nul 2>&1 fsutil dirty query %systemdrive% && (goto gotAdmin) || (goto Elevate_privileges)
:Elevate_privileges
:: Determine if Windows Terminal is installed.
set Terminal=cmd.exe
Powershell "Get-AppxPackage|Where Name -like '*Terminal*'"|findstr /i "Microsoft.WindowsTerminal" >nul
if "%errorlevel%"=="0" set Terminal=wt.exe
:: If gsudo is installed, use it.
if exist "C:\Program Files (x86)\gsudo\Current\gsudo.exe" sudo "%~s0" & exit /B
if exist "C:\Program Files\gsudo\Current\gsudo.exe" sudo "%~s0" & exit /B
:: If Windows Terminal is installed, use it.
if exist "%SYSTEMROOT%\System32\Cscript.exe" (
echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "%Terminal%", "cmd /c %~s0", "", "runas", 1 > "%temp%\getadmin.vbs"
cscript //nologo "%temp%\getadmin.vbs"
exit /b
) else (
powershell -Command "Start-Process -Verb RunAs -FilePath '%Terminal%' -ArgumentList 'cmd /c %~s0'"
exit /b
)
:gotAdmin
if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs"
pushd "%CD%" && CD /D "%~dp0"
cls
::===============================================================================
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
General debugging tips:
Remove "echo off"
Add pause command in some places
Run and see the output