JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Run the batch file as administrator

Author:JIYIK Last Updated:2025/03/20 Views:

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.

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.

Open the Properties window

Click “Advanced” under the “Shortcut” tab, and then select the “Run as administrator” check box.

Advanced Properties

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

Test file creation shortcut

The above code will change the current directory to the location of the batch file.

Output:

Test file creation shortcut 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"

Test file runas command

Output:

Output of runas command

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"

Test file runas file

The above code will run the batch file under the Administrator user. Enter the password if prompted.

You can also use to /savecredsave the password and use it the next time you run the batch file. You only have to enter the password once.

Output:

Output runas file

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

Test file creation vbs

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

%~dp0Change 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:

Test file creation vbs 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.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial