How to Request Administrator Access (UAC) Inside a Windows Batch File

Here is a way to automatically request administrator access and elevate UAC permissions in a batch file that requires them to run correctly.

By Tim Trott | Windows Tips and Tricks | July 4, 2012

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.

powershell
:: 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.

User Account Control (UAC) Prompt in Windows
User Account Control (UAC) Prompt in Windows

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.

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 2 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Learn how your comment data is processed.

  1. GO

    On Tuesday 6th of June 2023, godwin said

    it didnt work though, i just wanted to give admin permission to a shutdown and abort batch file,

  2. KA

    On Sunday 5th of March 2023, Kai said

    I don't know what language that is. I have seen this on multiple things. They are saying "This is bash."
    while you are saying "This is powershell."