Web Design that taps into the haromny and vision of your dreams.

How to Request Administrator Access Inside a Batch File

Request Administrator Access Elevation inside CMD/batch files

By on in Coding

198 words, estimated reading time 1 minutes.

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 a batch file that requires elevated privileges to run correctly.

This is equivalent to choosing "Run as Administrator" by right-clicking a batch file. In either case, the UAC prompt would still show up.

Copy and Paste this snippet into the top of the batch file to automatically prompt for admin rights if it does not already have them.

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
IF '%PROCESSOR_ARCHITECTURE%' EQU 'amd64' (
   >nul 2>&1 "%SYSTEMROOT%\SysWOW64\icacls.exe" "%SYSTEMROOT%\SysWOW64\config"
 ) ELSE (
   >nul 2>&1 "%SYSTEMROOT%\system32\icacls.exe" "%SYSTEMROOT%\system32\config"
)

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"
:--------------------------------------  

Last updated on: Wednesday 4th July 2018

 

Comments

Have a question or suggestion? Please leave a comment to start the discussion.

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.