How to Run Batch File as Administrator
- Create a Shortcut of the Batch File to Run as Administrator
- Code a Batch File to Run as Administrator
- Conclusion
A Batch file consists of commands that are executed by the command prompt. Some of these commands don’t run without administrator permission; hence, it is important to run a Batch file as an administrator.
Manually, you can run the Batch file as an administrator by right-clicking on the Batch file you want to run and selecting Run as administrator.
Instead of doing it manually each time, you can also make it run as an administrator automatically by just adding some code at the top of your batch file.
Alternatively, you can also create a shortcut and set it to run as administrator from the properties window. Every time you double-click on the Batch file, it will run as an administrator.
There are many ways of automatically running the Batch file as an administrator. This tutorial will illustrate different ways of running a batch file as an administrator.
Create a Shortcut of the Batch File to Run as Administrator
A simple way of running a Batch file in admin mode is by creating a shortcut and setting it up to always run as an administrator. Right-click on the Batch file and click on create shortcut
to create a shortcut.
A shortcut file will be created on the Desktop. Go to the Properties window of the shortcut file by right-clicking on it.
Click on Advanced under the Shortcut tab and select the Run as administrator checkbox.
That’s all, and the shortcut has been set to always run in the admin mode. When you double-click on the shortcut file, it will show a UAC window to confirm.
When you apply the above method, the current directory of the Batch file changes; this may cause errors, or your file may not run. Just add the following lines at the top of your Batch file to avoid this.
@setlocal enableextensions
@cd /d "%~dp0"
Example:
@echo off
@setlocal enableextensions
@cd /d "%~dp0"
::START OF THE PROGRAM::
echo "Check the system's energy efficiency"
powercfg-energy
The above code will change the current directory to the Batch file’s location.
Output:
Code a Batch File to Run as Administrator
Use the runas
Command to Run the Batch File as Administrator
If the Batch file contains a particular line or a set of lines that requires administrative privileges, you can use the runas
command to run a particular line in admin mode.
@echo off
echo "Check the system's energy efficiency"
runas /user:sid "cmd /k ipconfig"
Output:
or
@echo off
@setlocal enableextensions
@cd /d "%~dp0"
echo "Check the system's energy efficiency"
runas /user:sid "notepad C:\Users\sid\Desktop\testfile.bat"
The above code will run the Batch file under an administrator user. Enter the password if prompted.
You can also use /savecred
to save the password and use it next time you run the Batch file. You only need to enter the password once.
Output:
Create a VBS File to Run the Batch File as Administrator
Creating a shortcut to the Batch file also changed the current working directory.
You can, instead, add the following code at the top of your Batch file. This will run the Batch file in administrator mode and remain in the same directory.
set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\adminmode.vbs" del "%temp%\adminmode.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\adminmode.vbs" && "%temp%\adminmode.vbs" && exit /B )
Example:
set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\adminmode.vbs" del "%temp%\adminmode.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\adminmode.vbs" && "%temp%\adminmode.vbs" && exit /B )
echo "Check the system's energy efficiency"
powercfg -energy
The above code checks whether the file is running in administrator mode. If it doesn’t, it creates a VBS file adminmode.vbs
, which then reruns the Batch file in administrator mode using the runas
parameter.
To access the Batch file, we used cd /d "%~dp0"
, where:
d
- expands to the drive letterp
- expands to the path0
- expands to the full path
The %~dp0
changes the current directory to the Batch file’s directory. When you run the Batch file, it will run as an administrator by showing the UAC prompt for confirmation.
Output:
Conclusion
So, we discussed two different ways of running a Batch file as an administrator.
Also, there are other ways of running a Batch file in admin mode, such as using the elevate utility, converting the batch file to .exe
, or installing the sudo
command. But, the methods mentioned above are easy to implement, even if you are a beginner.