How to code a BAT file to always run as admin mode?
I would like to execute this in Administrator mode. How to modify the bat code to run this as admin? Is this correct? Do I need to put the quotes?
runas /user:Administrator invis.vbs Example1Server.exe
asked Jul 25, 2011 at 2:51
6,695 16 16 gold badges 65 65 silver badges 80 80 bronze badges
Apr 7, 2017 at 17:39
Apr 7, 2017 at 17:39
12 Answers 12
The other answer requires that you enter the Administrator account password. Also, running under an account in the Administrator Group is not the same as run as administrator see: UAC on Wikipedia
Windows 7 Instructions
In order to run as an Administrator, create a shortcut for the batch file.
- Right click the batch file and click copy
- Navigate to where you want the shortcut
- Right click the background of the directory
- Select Paste Shortcut
Then you can set the shortcut to run as administrator:
- Right click the shortcut
- Choose Properties
- In the Shortcut tab, click Advanced
- Select the checkbox «Run as administrator»
- Click OK, OK
Now when you double click the shortcut it will prompt you for UAC confirmation and then Run as administrator (which as I said above is different than running under an account in the Administrator Group)
Check the screenshot below

Note: When you do so to Run As Administrator, the current directory (path) will not be same as the bat file. This can cause some problems in many cases that the bat file refer to relative files beside it. For example, in my Windows 7 the cur dir will be SYSTEM32 instead of bat file location! To workaround it, you should use
to ensure cur dir is at the same path where the bat file is.
How to create a batch file to run cmd as administrator
I need to run a batch file which needs to register a DLL. The DLL registration is failing because the Batch file is not starting the command prompt as «administrator». Is there any way to start the «Command Prompt» as administrator through the batch file. Environment: Win7/Vista
21.3k 9 9 gold badges 47 47 silver badges 61 61 bronze badges
asked Jul 17, 2012 at 14:44
1,421 4 4 gold badges 24 24 silver badges 34 34 bronze badges
Jul 17, 2012 at 14:44
May 13, 2013 at 14:50
@EdGreaves is that link not to this question?
Oct 20, 2017 at 9:46
Nov 29, 2017 at 15:28
The question is ambiguous: is the question to run it as the user Administrator, or to run it with administrative rights?
Oct 26, 2018 at 18:59
13 Answers 13
This script does the trick! Just paste it into the top of your bat file. If you want to review the output of your script, add a «pause» command at the bottom of your batch file.
This script is now slightly edited to support command line args.
@echo off :: BatchGotAdmin ::------------------------------------- REM --> Check for permissions >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" REM --> If error flag set, we do not have admin. if '%errorlevel%' NEQ '0' ( echo Requesting administrative privileges. goto UACPrompt ) else ( goto gotAdmin ) :UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" set params = %*:"=" echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" exit /B :gotAdmin pushd "%CD%" CD /D "%~dp0" ::-------------------------------------- ::ENTER YOUR CODE BELOW:
1,736 18 18 silver badges 28 28 bronze badges
answered Mar 13, 2015 at 7:19
Lokesh Kumar Lokesh Kumar
819 2 2 gold badges 8 8 silver badges 13 13 bronze badges
this code is working for enabling windows update service. But not working for disabling windows update service sc stop wuauserv & sc config wuauserv start= disabled
Jan 2, 2017 at 18:24
Note: this code will occasionally not function if run from sub-directories of a different drive. See my question on it here: superuser.com/questions/1192426/…
Mar 26, 2017 at 2:58
OMG — this worked perfectly! I’m of an age where everything was batch files and DOS programming, and know most tricks but didn’t realise this was possible! I’m highly impressed! Thanks for sharing!
Jun 19, 2019 at 8:29
Calling the script with args: %* is set before the code but empty after the last line
Jun 5, 2023 at 8:34
such a tremendous boilerplate for a simple action!
Sep 28, 2023 at 18:37
You might have to use another batch file first to launch the second with admin rights.
In the first use
runas /noprofile /user:mymachine\administrator yourbatchfile.bat
Upon further reading, you must be able to type in the password at the prompt. You cannot pipe the password as this feature was locked down for security reasons.
You may have more luck with psexec.
answered Jul 17, 2012 at 14:47
30.8k 35 35 gold badges 124 124 silver badges 152 152 bronze badges
I will try to implement your solution,
Jul 17, 2012 at 14:51
I tried the above command, but it asks for administaor password that I wont be knowing .
Jul 17, 2012 at 16:13
@JChan It seems that you can’t automatically fill the password, this feature has been disabled, see my updated answer.
Jul 17, 2012 at 16:23
Press Ctrl + Shift and double-click a shortcut to run as an elevated process.
Works from the start menu as well.
15.1k 27 27 gold badges 135 135 silver badges 194 194 bronze badges
answered May 18, 2015 at 8:13
caractacus caractacus
125 1 1 silver badge 2 2 bronze badges
(This is based on @DarkXphenomenon’s answer, which unfortunately had some problems.)
You need to enclose your code within this wrapper:
if _%1_==_payload_ goto :payload :getadmin echo %~nx0: elevating self set vbs=%temp%\getadmin.vbs echo Set UAC = CreateObject^("Shell.Application"^) >> "%vbs%" echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%" "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" goto :eof :payload echo %~nx0: running payload with parameters: echo %* echo --------------------------------------------------- cd /d %2 shift shift rem put your code here rem e.g.: perl myscript.pl %1 %2 %3 %4 %5 %6 %7 %8 %9 goto :eof
This makes batch file run itself as elevated user. It adds two parameters to the privileged code:
- word payload , to indicate this is payload call, i.e. already elevated. Otherwise it would just open new processes over and over.
- directory path where the main script was called. Due to the fact that Windows always starts elevated cmd.exe in «%windir%\system32», there’s no easy way of knowing what the original path was (and retaining ability to copy your script around without touching code)
Note: Unfortunately, for some reason shift does not work for %* , so if you need to pass actual arguments on, you will have to resort to the ugly notation I used in the example ( %1 %2 %3 %4 %5 %6 %7 %8 %9 ), which also brings in the limit of maximum of 9 arguments
Run Batch File as Administrator

- Create a Shortcut of the Batch File to Run as Administrator
- Code a Batch File to Run as Administrator
- Conclusion
A Batch file consists of commands that are executed by the command prompt. Some of these commands don’t run without administrator permission; hence, it is important to run a Batch file as an administrator.
Manually, you can run the Batch file as an administrator by right-clicking on the Batch file you want to run and selecting Run as administrator.
Instead of doing it manually each time, you can also make it run as an administrator automatically by just adding some code at the top of your batch file.
Alternatively, you can also create a shortcut and set it to run as administrator from the properties window. Every time you double-click on the Batch file, it will run as an administrator.
There are many ways of automatically running the Batch file as an administrator. This tutorial will illustrate different ways of running a batch file as an administrator.
Create a Shortcut of the Batch File to Run as Administrator
A simple way of running a Batch file in admin mode is by creating a shortcut and setting it up to always run as an administrator. Right-click on the Batch file and click on create shortcut to create a shortcut.

A shortcut file will be created on the Desktop. Go to the Properties window of the shortcut file by right-clicking on it.

Click on Advanced under the Shortcut tab and select the Run as administrator checkbox.

That’s all, and the shortcut has been set to always run in the admin mode. When you double-click on the shortcut file, it will show a UAC window to confirm.
When you apply the above method, the current directory of the Batch file changes; this may cause errors, or your file may not run. Just add the following lines at the top of your Batch file to avoid this.
@setlocal enableextensions @cd /d "%~dp0"
@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 Batch file’s location.

Code a Batch File to Run as Administrator
Use the runas Command to Run the Batch File as Administrator
If the Batch file contains a particular line or a set of lines that requires administrative privileges, you can use the runas command to run a particular line in admin mode.
@echo off echo "Check the system's energy efficiency" runas /user:sid "cmd /k ipconfig"


@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 an administrator user. Enter the password if prompted.
You can also use /savecred to save the password and use it next time you run the Batch file. You only need to enter the password once.

Create a VBS File to Run the Batch File as Administrator
Creating a shortcut to the Batch file also changed the current working directory.
You can, instead, add the following code at the top of your Batch file. 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 )
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 whether the file is running in administrator mode. If it doesn’t, it creates a VBS file adminmode.vbs , which then reruns the Batch file in administrator mode using the runas parameter.
To access the Batch file, we used cd /d «%~dp0» , where:
- d — expands to the drive letter
- p — expands to the path
- 0 — expands to the full path
The %~dp0 changes the current directory to the Batch file’s directory. When you run the Batch file, it will run as an administrator by showing the UAC prompt for confirmation.

Conclusion
So, we discussed two different ways of running a Batch file as an administrator.
Also, there are other ways of running a Batch file in admin mode, such as using the elevate utility, converting the batch file to .exe , or installing the sudo command. But, the methods mentioned above are easy to implement, even if you are a beginner.
Related Article — Batch File
- Rename Part of Filename in Batch Script
- Run Batch File Remotely
- Transfer File to Raspberry Pi Using Batch Script
- Run Multiple Files in Batch Script
- New Line in Batch Script
- Use Batch File to Write to a Text File
Copyright © 2024. All right reserved
Automatically running a batch file as an administrator
How do I run a batch file from another batch file with administrator rights? I have tried the RUNAS command, but it requires the administrator password. I am searching for an alternative for running a batch file by right clicking on it and running as an administrator. I want to automate it from another batch file.
30.8k 22 22 gold badges 106 106 silver badges 131 131 bronze badges
asked Sep 12, 2013 at 4:48
590 1 1 gold badge 5 5 silver badges 10 10 bronze badges
user93353 gave you the answer, there is no else way. For security reason. Another option would be to give rights to your user to execute or write at the resource you are looking for. If you can, please give more details on want you want to achieve.
Sep 12, 2013 at 4:58
possible duplicate of Windows CMD Batch File Run A Command as Administrator
Sep 12, 2013 at 4:59
@Endoro — it’s not a dup — he is asking how to do it non-interactively — the answer in the other does it interactively.
Sep 12, 2013 at 5:11
Thanks for the reply guys. I used UAC control of windows to disable the authentication so that i can run Batch files from batch files.
Nov 17, 2014 at 8:34
Does this answer your question? How to code a BAT file to always run as admin mode?
Nov 5, 2019 at 8:57
9 Answers 9
Put each line in cmd or all of theme in the batch file:
@echo off if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b) "Put your command here"
it works fine for me.
1,215 2 2 gold badges 12 12 silver badges 20 20 bronze badges
answered Jul 23, 2018 at 5:34
Hossein Jafarinejad Hossein Jafarinejad
511 4 4 silver badges 7 7 bronze badges
caution: it works, but when running the batch-file then the working-directory of your commands will be C:\WINDOWS\system32 . usually (without the elevation) the working-directory of your batch-file would be the folder where the batch-file is located in.
Nov 17, 2020 at 8:44
- Create a shortcut to that batch file
- Right click on that shortcut file and choose Properties
- Click the Advanced button to find a checkbox for running as administrator
Check the screenshot below

30.8k 22 22 gold badges 106 106 silver badges 131 131 bronze badges
answered Feb 10, 2015 at 14:24
5,363 3 3 gold badges 38 38 silver badges 52 52 bronze badges
Bad answer. OP asked for a way to run the batch file as admin from within another batch file, this doesn’t work that way.
– user3856347
May 2, 2016 at 20:12
@prodigy, Have you tried and confirmed that it doesn’t worked?
May 2, 2016 at 23:04
I probably should’ve specified that I tried this on XP a while back, it didn’t work. I haven’t tried it recently so I may be wrong, or I may have done it incorrectly when I did.
– user3856347
May 4, 2016 at 19:01
You can use PowerShell to run b.bat as administrator from a.bat :
set mydir=%~dp0 Powershell -Command "& < Start-Process \"%mydir%b.bat\" -verb RunAs>"
It will prompt the user with a confirmation dialog. The user chooses YES , and then b.bat will be run as administrator.
30.8k 22 22 gold badges 106 106 silver badges 131 131 bronze badges
answered Dec 15, 2014 at 7:40
bismack163 bismack163
226 2 2 silver badges 4 4 bronze badges
runas /savecred /profile /user:Administrator whateveryouwanttorun.cmd
It will ask for the password the first time only. It will not ask for password again, unless the password is changed, etc.
answered Sep 12, 2013 at 4:54
13.8k 8 8 gold badges 61 61 silver badges 122 122 bronze badges
That will allow the user to run arbitrary commands with administrative privileges, so you could just as well make him a member of the local administrators group.
Sep 12, 2013 at 7:13
@AnsgarWiechers — in his own words, I am searching for an alternative for running a batch file by right clicking on it and running as an administrator. i want to automate it from another batch file. — if he is able to run it by right clicking and run as administrator, then he already knows the admin password — this is like sudo on linux. Another thing is that this cannot be used without administrator password.
Sep 12, 2013 at 7:22
True, but irrelevant. sudo can be restricted to particular commands, runas /savecred can’t. If you remove the password-prompt you effectively remove the barrier between admin and normal user. A better approach might be creating a scheduled task.
Sep 12, 2013 at 8:15
@AnsgarWiechers — in this case, the user has access to root/admin password. In Linux, if he has root password, he can modify the sudoers file to remove any restriction on sudo.
Sep 12, 2013 at 8:20
AFAIK, runas.exe doesn’t provoke an elevation prompt, which appears to be what the OP is referring to.
Jul 30, 2014 at 15:35
If you’re trying to invoke a Windows UAC prompt (the one that puts the whole screen black and asks if you’re granting administrator privileges to the following task), RUNAS is not the smoothest way to do it, since:
- You’re not going to get prompted for UAC authorization, even if logged in as the administrator and
- RUNAS expects that you have the administrator password, even if your user is setup as a local administrator, in which case the former password is not a sound security practice, specially in work environments.
Instead, try to copy & paste the following code to ensure that your batch file runs with administrator privileges:
@echo off >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" if '%errorlevel%' NEQ '0' ( echo Requesting Admin access. goto goUAC ) else goto goADMIN :goUAC echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" set params = %*:"="" echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" exit /B :goADMIN pushd "%CD%" CD /D "%~dp0" rem --- FROM HERE PASTE YOUR ADMIN-ENABLED BATCH SCRIPT --- echo Stopping some Microsoft Service. net stop sqlserveragent rem --- END OF BATCH ----
This solution works 100% under Windows 7, 8.1 and 10 setups with UAC enabled.