Automatically run batch bat files as administrator (VBS and bat)

Time:2021-9-24

However, the batch script will not be run as an administrator by default. Generally, I will name the script “XXXXXX (please right-click and run as an administrator!). Bat”, but some users will ignore this prompt and run directly. At this time, the script fails due to insufficient permissions.

We can use an alternative method. When bat starts, we first call vbs script. Through vbs script, we can call the runas part of the bat as an administrator

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

The example code is as follows

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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
 
:: fill in your own script
 
After echo execution, press any key to exit
 
pause >nul
exit

VBS starts bat2 with administrator privileges

?
1
2
3
4
5
6
Set objShell = CreateObject(“WScript.Shell”)
Strpath = replace (wsscript.scriptfullname, filename, "")
objShell.Run("runas /user:administrator " & strPath & “cmd.Bat”)
WScript.Sleep 1000
objShell.SendKeys “password{ENTER}”
WScript.Sleep 1000

Note: runas cannot pass parameters

Execute commands with administrator privileges in the bat script

Set the written bat to run after startup, but it does not run as an administrator by default, so some commands cannot be executed during startup

First of all: if windows will pop up every time it obtains administrator privileges, close the pop-up window first

The closing method is that each time a prompt pops up, the prompt box has a dark font. You can modify the pop-up settings, click in, and set it to never or no pop-up window

Next, you only need to add a little code to the. Bat script to run as an administrator

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

:: enforce administrator run permissions

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

How can CMD / Bat automatically request administrator privileges without right clicking the administrator

Complete code first:

?
1
2
3
4
5
6
7
8
9
10
11
12
@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo Request administrator privileges
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

The principle is as follows:

Access a folder that requires authorization, such as (% systemroot% \ system32 \ config \ system)

If it is allowed, it indicates that it obviously has administrator privileges

If rejected, you have to apply for permission.

To apply for permission:

(see the echo code)

Create a runner to run programs in the relative directory (the relative directory is the directory where batch processing is run) with administrator privileges.

Then you will naturally apply for an administrator. Wow, that’s all for this article about VBS automatically running batch processing (BAT) files as an administrator. For more information about running batch processing as an administrator, please search the previous articles of developepper or continue to browse the relevant articles below. I hope you can support developepper in the future!