I have created a batch file for starting a service using sc.exe. But it asks for admin privilege while running. Is there is any code that can be used in batch file to elevate cmd to admin level?
Asked
Active
Viewed 2,935 times
-1
-
2Possible duplicate of [How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?](http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-administrator) – SomethingDark Jan 27 '16 at 06:34
2 Answers
0
Press Windows Key + X
and click Command Prompt (Admin)
. Click yes if the user control center ask for permission

NiroshanJ
- 558
- 1
- 5
- 20
-
-
-
I know. Win+X is not a valid shortcut in Windows 7 or earlier, so this might not help some people. – SomethingDark Jan 27 '16 at 06:40
-
Ah yes. and shijin just ask for code. I didn't see it. This may not the answer :) – NiroshanJ Jan 27 '16 at 07:24
-
I am trying to run a code from batch file. Is it possible to run cmd as admin without doing this? – shijin Jan 27 '16 at 08:22
-
Is there is any code for running cmd as admin without asking for permission? – shijin Jan 27 '16 at 08:22
0
You can do this:
@echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if %errorlevel% NEQ 0 (GOTO askAdmin)
GOTO gotAdmin
:askAdmin
::batch is being ran as normal user
echo I'm not an admin yet
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo Requesting administrative 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
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
::batch is being ran as admin
echo Now I'm an admin!
pause

Dennis van Gils
- 3,487
- 2
- 14
- 35
-
I used this but it is asking permission in between execution. Is it possible to avoid this situation..i.e, run cmd as admin without asking permission, – shijin Jan 27 '16 at 08:21
-
@shijin you can run it as admin from the start by right clicking on it or using the shortcut mentioned in the other answer, but this is the only way to do it in the program itself – Dennis van Gils Jan 27 '16 at 08:24
-
Is there is any way to get admin privilege from code itself? Without clicking run as admin? – shijin Jan 27 '16 at 08:35
-
@shijin That is what the code I provided does. However, it would be weird if a program could give itself admin priviliges without needing to ask for them. That would be a huge security threat, imagine a virus giving itself admin rights without needing confirmation from you. – Dennis van Gils Jan 27 '16 at 12:26
-
my project is a forensic project. the project is to acquire the ram dump directly while plugging in a usb drive. while dumping there should be minimal user interactions. – shijin Jan 29 '16 at 07:18