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

Written By on in C#

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
   >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 )
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B
    pushd "%CD%"
    CD /D "%~dp0"

Last updated on: Wednesday 4th July 2018



There are no comments for this post. Be the first!


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me