If you are not aware of what is a batch file, a batch file (also referred as batch script, bat file or DOS command file) is a text-based file that ends with the extension “*.bat”(sometimes it can be *.cmd file). A batch file necessarily contains one or multiple DOS commands (DOS commands are only operable on old DOS and new Windows OS). It is named “Batch” because it is a collection (batch) of commands. Batch files are useful as they can be used to command or run multiple instructions on a computer at a once or sequentially. Also, since they are text files, it is very easy to write and edit batch files. A big upside is, you don’t need to be a programmer to execute some simple commands to your computer if you want to do it using batch file. Simply, a batch file can have the same codes we write on Windows command prompt.
Necessity of Running as Administrator
As a batch file is a bunch of commands to instruct some tasks on DOS or Windows, sometimes you may need to execute commands in administrator mode. This helps the commands run in administrative privilege, thus making the instructions work with greater access.
Traditional Methods and Problems
As Windows do not run bat files as administrator, we have to use the right-click context menu to run it as admin. Another common method is to create a shortcut to the bat file and invoke the shortcut to run as administrator. Other methods include calling the file from anther program. The problem is, all these methods require manual action. That means you need to rely on a secondary system to make any batch file run with administrative privilege.
Hopefully I have got two ideas to create a batch file that will run as administrator itself without the need of another instruction or file. Both methods works differently. One method is to adding some lines of code to your bat file, where another is, converting your bat file into an executable file.
Method 1: Adding a Snippet of Code to the Batch File
Though there’s no native batch code to run a bat file itself as administrator without a helper file, there are some working tricks that can make it happen. Evan Greene has made a such workaround code that lets you run any batch script as administrator (with UAC prompt) by default.This code snippet will automatically force any .BAT or .CMD batch file use the Windows UAC prompt while running. Here’s the code that you need to paste before the original codesin your BAT file.
:: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
) else ( goto gotAdmin )
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
CD /D "%~dp0"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line
Any bat file containing the above code should prompt a Windows UAC window to run it as administrator. You neither have to compromise the bat file, nor depend on another file. Or alternatively you can download our sample .BAT file at BatGotAdmin.zip. After downloading the file, extract the bat file and add your own code without touching the admin executor code. I hope all should appreciate this method and credits goes to Evan Greene.
Method 2: Converting the BAT into EXE file with Admin Manifest added
Yes your guess is right. You can convert your batch file into an executable exe file where the program will run with admin access. There’s an awesome free tool named Advanced BAT to EXE that doesn’t only convert to EXE but also has numerous features. Here, I don’t have space to describe the extra features; rather I would show you how you can simply convert your BAT into a EXE file that will run as Admin.
How to Convert a BAT file to EXE Program and make it Run as Admin
- Download Advanced BAT to EXE tool. Install and run it. In the field, you can write batch codes or load a batch file from File > Open.
- Once you have done loading your batch codes, go to File > Build EXE. A pop-up window will come, asking you enter the information about the output executable file. Here, most important part is, add tick mark next to Include Administrator Manifest. After you enabled the admin manifest option, review the settings and hit Build EXE.
- Give the file a name and click Save. That’s it. The output file will be an executable EXE form of your input BAT file. Nice?
Converting a Batch file to an exe file explicitly hides your codes. It gives you more comfort and control over the file. However you cannot edit EXE files as you do BATs.
Things get a lot easier when you are able to run your batch file automatically as administrator itself using in-house codes without needing of a secondary file. If you want to hide your codes, use the second method, or the first method is recommended.