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. Method One
Some computers are logged in with non-administrator accounts and require a prompt to confirm administrator privileges when running programs. The solution is as follows:
@echo off
%1 %2
ver|find "5.">nul&&goto :Admin
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof
:Admin
:: Add the above command at the beginning of your BAT file.
:: Below are the commands you need to execute.
set path=%~dp0
echo Install MySQL service...
echo %path%
cd %path%\bin\
mysqld.exe --remove mysql
mysqld.exe --install mysql
echo Start MySQL service
"%SystemRoot%"\system32\net start mysql
2. 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"
3. Running Commands as Administrator in BAT Scripts
Add the following code at the beginning of your BAT file:
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
- Automatically Running BAT Files as Administrator
In day-to-day operations, batch scripts (BAT files) are a simple and quick way to install or configure systems on Windows. However, batch scripts do not run as administrator by default.
To address this, you can name the script something like XXXXXX (Please right-click and run as administrator!).bat
. However, some users may still ignore the prompt and run the script directly, causing it to fail due to insufficient privileges.
To solve this, you can use a workaround by creating a VBS script that ensures the BAT file is run with administrator privileges.
Here’s an example script:
@ECHO OFF
setlocal EnableDelayedExpansion
color 3e
title Add Service Configuration
PUSHD %~DP0 & cd /d "%~dp0"
%1 %2
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :runas","","runas",1)(window.close)&goto :eof
:runas
:: Add your own script here
echo Task completed, press any key to exit
pause >nul
exit
5. Running BAT Files with Administrator Privileges
Simply add the following script at the beginning of your BAT file. When you double-click the file, it will automatically run with administrator privileges:
@echo off&color 17
if exist "%SystemRoot%\SysWOW64" path %path%;%windir%\SysNative;%SystemRoot%\SysWOW64;%~dp0
bcdedit >nul
if '%errorlevel%' NEQ '0' (goto UACPrompt) else (goto UACAdmin)
:UACPrompt
%1 start "" mshta vbscript:createobject("shell.application").shellexecute("""%~0""","::",,"runas",1)(window.close)&exit
exit /B
:UACAdmin
cd /d "%~dp0"
echo Current working path: %CD%
echo Administrator privileges obtained
6. Automatically Running BAT or CMD Scripts as Administrator
Some batch scripts require elevated privileges to execute commands effectively. To do so, add the following code at the beginning of your script:
@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else (
goto gotAdmin
)
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" (
del "%temp%\getadmin.vbs"
)
7. Correct Method for Automatically Running CMD Scripts as Administrator
To handle paths with spaces, add the following code at the beginning of your script:
- For scripts with no parameters:
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c "^&chr(34)^&"%~0"^&chr(34)^&" ::","%cd%","runas",1)(window.close)&&exit
- For scripts with one parameter:
%2 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c "^&chr(34)^&"%~0"^&chr(34)^&" "^&chr(34)^&"%~1"^&chr(34)^&" ::","%cd%","runas",1)(window.close)&&exit
For additional parameters, follow the same logic.
8. 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".
- Open CMD as administrator and run the BAT file from the command prompt.
This concludes the methods for running batch scripts with administrator privileges.
Top comments (0)