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 TrottWindows Tips and Tricks • July 4, 2012
How to Request Administrator Access (UAC) Inside a Windows Batch File

To manually elevate the permissions for batch files, you would right-click on it and choose Run as Administrator, but shown here is a way in which you can automatically elevate and request administrator access and continue running a script.

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 makes it easy to double-click the file to run. In either case, the UAC prompt would still 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 UAC will block them 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 to run as administrator.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. 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 comments. Why not join the discussion!

New comments for this post are currently closed.