Run the batch file as administrator
Batch files consist of commands that are executed by the Command Prompt. Some of these commands cannot be run without administrator privileges; therefore, it is important to run batch files as an administrator.
You can manually run a batch file as an administrator by right-clicking the batch file you want to run and selecting "Run as Administrator".
You can also make it automatically run as administrator by adding some code at the top of your batch file instead of doing it manually every time.
Alternatively, you can also create a shortcut and set it to run as administrator from the properties window. Every time you double-click the batch file, it will run as administrator.
There are various ways to automatically run a batch file as an administrator. This tutorial will explain the different ways to run a batch file as an administrator.
Create a shortcut to the batch file to run as administrator
An easy way to run a batch file in administrative mode is to create a shortcut and set it to always run as an administrator. Right-click the batch file and click Create Shortcut to create a shortcut.
A shortcut file will be created on the desktop. Right-click the shortcut file to go to the Properties window of the shortcut file.
Click “Advanced” under the “Shortcut” tab, and then select the “Run as administrator” check box.
That’s it, the shortcut has been set to always run in Admin mode. When you double-click the shortcut file, the UAC window will appear for confirmation.
When you apply the above method, the current directory of the batch file changes; this may cause errors, or your file may not run. Simply add the following line 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 location of the batch file.
Output:
Write a batch file to run as administrator
Use the runas command to run the batch file as an administrator
If your batch file contains a specific line or set of lines that require administrative privileges, you can use the runas command to run the specific lines in administrative 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 the Administrator user. Enter the password if prompted.
You can also use to /savecred
save the password and use it the next time you run the batch file. You only have 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 changes the current working directory.
You can add the following code at the top of your batch file instead. 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 if the file is running in administrator mode. If not, it creates a VBS file adminmode.vbs and then reruns the batch file in administrator mode using the runas parameter.
To access the batch file, we use cd /d "%~dp0"
, where:
- d - expands to the drive letter
- p - expands to the path
- 0 - expands to the full path
%~dp0
Change the current directory to the directory of the batch file. When you run the batch file, it will run as an administrator and display the UAC prompt for confirmation.
Output:
Summarize
So, we have discussed two different ways to run a batch file as an administrator.
There are other ways to run a batch file in admin mode, such as using the elevate utility, converting the batch file to .exe, or installing the sudo command. However, the above method is easy to implement even if you are a beginner.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a file exists using batch processing
Publish Date:2025/03/20 Views:100 Category:OPERATING SYSTEM
-
This article will demonstrate how to use a batch script to check if a file exists through sample code. Check if a file exists using batch script The general format or syntax of the code to check if a file exists is provided below. IF EXIST
Running multiple files in a batch script
Publish Date:2025/03/20 Views:55 Category:OPERATING SYSTEM
-
Large scripts contain multiple files because it is easy to maintain the code. When working with larger scripts, you may want to divide them into modules to make it easier to detect any coding errors or problems. However, Batch does not have
Difference between % and %% in batch files
Publish Date:2025/03/20 Views:200 Category:OPERATING SYSTEM
-
Batch programmers often confuse single percent signs (%) and double percent signs (%%) when used in batch files. The FOR command uses %f when executed on the command line, but in a batch file, it uses %%f instead of a single percent sign. T
Error handling in batch scripts
Publish Date:2025/03/20 Views:127 Category:OPERATING SYSTEM
-
Every scripting and programming language contains error handlers, for example Java contains try-catch for error handling. In batch scripts, there is no direct way to do this, but we can create error handlers in batch scripts using the built
Waiting for commands in a Windows batch file to finish executing
Publish Date:2025/03/20 Views:98 Category:OPERATING SYSTEM
-
There are multiple commands and installation processes in a batch file, which usually takes some time to complete. However, when a batch file is run, it does not wait for the command process to complete; it executes all the commands line by
Redirecting output to a text file from within a batch file
Publish Date:2025/03/20 Views:186 Category:OPERATING SYSTEM
-
This article will explain different ways to redirect output from a text file. Redirection operators in batch scripts We can redirect the output of a batch file to a text file using redirection operators. Redirection operators redirect the i
Ignore case in Linux grep
Publish Date:2025/03/20 Views:166 Category:OPERATING SYSTEM
-
In this tutorial, we will learn grep to ignore case in file names using . But first, let's grep start with . Use grep to search each file for a pattern. Patterns lists the patterns separated by newlines, grep outputting each line that match
Adding an empty directory in Git
Publish Date:2025/03/20 Views:167 Category:OPERATING SYSTEM
-
One of the most important things about git is that it only tracks changes to files, not the folders themselves, which can be found here. Directories are automatically added when you add a file in a directory. Also, when you run git add git
从 Powershell 脚本运行 BAT 文件
Publish Date:2024/03/15 Views:381 Category:编程语言
-
本教程说明了从 PowerShell 脚本运行 BAT 文件的不同方法。