Skip to content

Instantly share code, notes, and snippets.

@Malix-off
Last active October 18, 2022 16:07
Show Gist options
  • Save Malix-off/d86db79ee44d32ef04841f4dba508aec to your computer and use it in GitHub Desktop.
Save Malix-off/d86db79ee44d32ef04841f4dba508aec to your computer and use it in GitHub Desktop.
Batch - Script Require Administrative Permissions Template
@echo off
:Initialisation
REM YOUR INITIALISATION CODE HERE
:Administrator_Permission
REM Complying with Administrator Permissions
:Check
REM Trying Running an Action Requiring Administrative Permissions, Then Store its Standard Error Output, OS-Dynamically
if "%PROCESSOR_ARCHITECTURE%" equ "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) else (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
:Condition
REM If the Previous Header Catch an Error, it means that Runtime Does Not Have Administrative Permissions
if '%errorlevel%' NEQ '0' (
echo This script requires administrative privileges.
echo.
echo Requesting administrative privileges...
goto UAC_Prompt
) else (
goto Got_Admin
)
:UAC_Prompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params= %*
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /b
:Got_Admin
pushd "%cd%"
cd /d "%~dp0"
:Main
REM YOUR MAIN CODE HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment