How to use a Windows Script with admin rights built in

Last Updated on 9 de Janeiro, 2021 by Vítor Fernandes

A batch file is a text file containing lines with commands that can be executed sequentially by the MS-DOS or Windows command interpreter. They are identified by the extensions .bat or .cmd.


Place this code right after @echo off

This will ask for admin permission rights after executing the script. Fantastic, don’t you think?

> nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

if '%errorlevel%' NEQ '0' (
    goto AbrirUAC
) else ( 
    goto AbrirBatch

echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\privilegio.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\privilegio.vbs"
exit /b 0

if exist "%temp%\privilegio.vbs" ( 
    del "%temp%\privilegio.vbs" 
pushd "%cd%"
cd /d "%~dp0"