How to run a BAT batch file with administrator privileges
The following information is obtained from the website:
https://dev.to/excalibra/the-method-to-run-a-bat-batch-file-with-administrator-privileges-16a
This article mainly introduces methods to run batch scripts (BAT files) with administrator privileges. Those who need this information can refer to the following steps:
1. Granting Administrator Privileges to BAT Scripts
To ensure a BAT script runs with administrator privileges, add the following code at the top of your BAT file:
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit cd /d "%~dp0"
Example:
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit cd /d "%~dp0"
echo Stopping WampManager...
taskkill /f /im wampmanager.exe
echo Waiting for process to terminate...
timeout /t 2 /nobreak
echo Starting WampManager...
cd /d C:\wamp64
start "" wampmanager.exe
echo Done
exit
2. Other Methods to Run BAT Files as Administrator
•Create a shortcut for the BAT file. Right-click the shortcut → Properties → Advanced → Tick "Run as Administrator".
•Convert the BAT file into an EXE using a BAT-to-EXE tool, then right-click the EXE → Properties → Compatibility → Tick "Run as Administrator".
○To convert BAT-to-EXE it's not pretty, but it's clean (nothing to clean up afterwards) and it's actually built-in to your system!
▪In your C:\Windows\System32\ folder, there is a file called iexpress.exe.
▪Right-click it an Run as administrator.
▪Create a new SED and select "Extract files and run an installation command."
▪Add the script you want, and make sure that on the next screen, you set the install program to cmd /c [your_script.bat] where [your_script.bat] is the script file you want to execute. If you don't do this, windows will try to use Command.com (the old version of Command Prompt) which hasn't been in use for quite a while.
▪Select preferences (you might need to select "Store files using Long File Name inside Package), set an output path (to the .exe file you want to create), and select "No restart".
▪Select Don't save for Save Self Extraction Directive
▪Click next and you should have your .exe!
Topic last reviewed: 15/09/25