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.
This question is related to
batch-file
The complete solution I found that worked was:
@echo off
cd /D "%~dp0"
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
"Put your command here"
credit for: https://stackoverflow.com/a/51472107/15087068
On Windows 7:
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
CMD Itself does not have a function to run files as admin, but powershell does, and that powershell function can be exectuted through CMD with a certain command. Write it in command prompt to run the file you specified as admin.
powershell -command start-process -file yourfilename -verb runas
Hope it helped!
This a trick that i used if anyone wants they can try this in batch file.This will give you the admin prompt when you run the batch file
@echo off
cd \ && cd windows/system32 && command which needs admin credentials
pause
Use
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.
Runas.exe won't work here. You can use VBScript to invoke the "Run as Administrator" shell verb. The Elevation Powertoys contain a batchfile that allows you to invoke an elevated command:
elevate
cmd.exe
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.
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.
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:
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.
Source: Stackoverflow.com