How to Request Administrator Access (UAC) Inside a Windows Batch FileHere is a way to automatically request administrator access and elevate UAC permissions in a batch file that requires them to run correctly.
To elevate the permissions for batch files manually, you would right-click on it and choose Run as Administrator.
Here is a way to automatically elevate and request administrator access from within a batch file that requires elevated UAC (User Account Control) privileges to run correctly. This is equivalent to choosing "Run as Administrator" by right-clicking a batch file, however, this method is easy to double-click the file to run. In either case, the UAC prompt would still show up to prompt for admin permission.
Copy and Paste this snippet into the top of the batch file to automatically prompt for admin rights if the script does not already have admin rights.
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
IF '%PROCESSOR_ARCHITECTURE%' EQU 'amd64' (
>nul 2>&1 "%SYSTEMROOT%SysWOW64icacls.exe" "%SYSTEMROOT%SysWOW64config"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%system32icacls.exe" "%SYSTEMROOT%system32config"
)
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"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%getadmin.vbs"
"%temp%getadmin.vbs"
del "%temp%getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
What is UAC and why do you need to Request Administrator Access?
User Account Control (UAC) helps prevent malware from damaging a computer and helps organizations deploy a better-managed desktop environment. With UAC, apps and tasks always run in the security context of a non-administrator account, unless an administrator specifically authorizes administrator-level access to the system.
Some tasks, especially those that update or modify system settings or files, will not function without administrative rights because they will be blocked by UAC to protect the system. It is then up to the user to grant that application sufficient permission for that task via the request administrator access screen. This manual intervention helps prevent malware from causing damage, however, it does rely on the user not blindly clicking yes to allow anything and everything from running as administrator.