2

I am working on a small batch script that elevates basic windows programs to run as administrator.

All of the functions work fine but I have ran into a problem when trying to run "regedit.exe".

When option 3 is selected in the script and it should run regedit as admin I get error:

740: The requested operation requires elevation.

Here is the script:

echo off
cls
echo.
echo Start as admin:
echo /----------------\
echo # 1. CMD         #
echo # 2. Task Manager#
echo # 3. Regedit     #
echo # 0. Exit        #
echo \----------------/
echo.

set /p run=

if %run%==1 (
set run="cmd"
)

if %run%==2 (
set run="taskmgr"
)

if %run%==3 (
set run="regedit"
)

if %run%==0 (
exit
)

runas /savecred /user:admin %run%

Is there a workaround for this?

I would like make this working without installing third party programs, modifying the registry or disabling UAC.

4 Answers 4

4

You can pull it off by launching a new CMD prompt with RunAs, which you tell to launch your command.

To do this in your batch, change the last line to:

runas /savecred /user:admin "cmd /c %run%"

2
  • PS: Check out choice /?. :) Feb 3, 2016 at 5:14
  • I have one more question. Since I have implemented your method, my error handling doesn't work. What I mean by that is, if the command is not recognized, errorlevel is no longer set to 1 since it is run in another window. Is there a way to somehow make it 1 again if there is a typo in the input? (what I mean by that is: no choice numbers or valid CMD command is given)
    – Divin3
    Feb 25, 2016 at 2:26
2
Add this to the top of your Batch file and at the end just put %run% instead of (runas /savecred /user:admin %run%)


@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"
:--------------------------------------
1

Perhaps this can help, or maybe starting the entire batch as admin may help, done by creating another batch script containing:

runas /user:admin C:\Path_to_your_batch_file\name.bat
1
  • The reason I created this, is because the admin password is so long and I don't want to type it every time, so because of '/savecred' I don't have to. Tried to create another bat and run it from this one, it prompts me for the password and after I type it, I get RUNAS ERROR: Unable to run - ...\regedit.bat -2147467259: Unspecified error
    – Divin3
    Feb 3, 2016 at 4:22
0
  1. Right-Click C:\Windows\regedit.exe and choose Properties
  2. In the Compatibility-Tab choose "Run this program as an administrator"
1
  • This is what I wanted to avoid. We have 16 digit admin password and typing it every time even on my own PC is a bit annoying. Using the /savecred will only have to type it once and that's it.
    – Divin3
    Feb 16, 2016 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.