How to make bat batch run with administrator’s permission


1. The first method

Some computers are non administrator login, running the program, you need to prompt whether to run or not. The solutions are 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
//Add the above command at the beginning of your bat
//Here 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 Serviceo
"%SystemRoot%"\system32\net start mysql

2. Bat script to obtain administrator rights

@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"

Write it at the top of the bat file.

3. In bat script, execute the command with administrator’s permission

Add the following command to the first line of the bat script file:

%1 mshta vbscript:CreateObject(“Shell.Application”).ShellExecute(“cmd.exe”,"/c %~s0 ::","",“runas”,1)(window.close)&&exit

4. Automatically run batch (BAT) files as Administrator

In the daily operation and maintenance work, in order to facilitate the system installation or configuration of Windows users, the batch processing (BAT file) provided by windows is the most simple and fast method.

However, batch scripts will not run as administrator by default. Generally, I will name the script “XXXXXX (please right-click, run as administrator!). Bat”, but some users will ignore this prompt and run directly. At this time, due to insufficient permissions, the script fails to run. We can use a flexible method, let bat start, first call vbs script, through vbs script, as an administrator to call the run as part of the bat

Our script can be written under runas, so click the bat script and run as an administrator

The sample code is as follows

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
:: fill in your own script
Echo execution completed, any key to exit
pause >nul

5. Execute bat scripts with administrator privileges

Just add the following script at the beginning of your. Bat file, and then double-click the. Bat file to execute 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)
%1 start "" mshta vbscript:createobject("shell.application").shellexecute("""%~0""","::",,"runas",1)(window.close)&exit
exit /B
cd /d "%~dp0"
The current running path of echo is% CD%
Echo has obtained administrator rights

6. How to make bat and CMD batch run as Administrator

How to make bat and CMD batch run as administrator?

Some batch processes need to be executed with high permissions to achieve the desired results,

Put the following code at the top of the batch to run as an administrator.

@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
goto UACPrompt
) else ( goto gotAdmin )
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
exit /B
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )

7. The correct way to run command line CMD batch automatically as Administrator

Fixed a problem with spaces in the path

Add at the beginning of the batch:

For no parameters

%1 mshta vbscript:CreateObject(“Shell.Application”).ShellExecute(“cmd.exe”,”/c “^&chr(34)^&”%~0″^&chr(34)^&” ::”,”%cd%”,”runas”,1)(window.close)&&exit

For a 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

And so on for more parameters
If you add an additional parameter::, this sentence will not be executed
In order to be compatible with 8.3 short paths, the%~0And so on%~s0etc.
Theoretically, it’s OK, but sometimes the boot path is unreliable, and then it may be necessarypushdorcd /d

8. Several ways to run bat file with administrator’s permission

1. Create a bat shortcut, and then right-click the shortcut — > Properties > Advanced — > run as administrator.
2. Download the tool from bat to exe, convert bat to exe, and then right-click exe > Properties > compatibility > run as administrator.
3. Run CMD with administrator privileges, and then run the corresponding bat in the DOS box.

This article introduces the implementation method of running bat batch with administrator’s permission. For more related administrator permission to run bat, please search the previous articles of developeppaer or continue to browse the following related articles. I hope you can support developeppaer more in the future!