none
Request Elevation inside CMD/batch file

    Question

  • Is it possible to request elevation inside a CMD/batch file? I have a Command Script( .cmd) and one of the command require admin right to run.
    I am NOT looking for right-click "Run as administrator", I would like the script itself to call the UAC prompt.

    Thank you,

    Ray
    Wednesday, March 17, 2010 8:45 PM

Answers

All replies

  • Hi Ray

    How about "runas /user:Administrator" in the command?

    Trent

    • Proposed as answer by C.Claude Friday, September 15, 2017 8:03 PM
    Thursday, March 18, 2010 1:24 AM
  • Hi,

    To elevate the permission, please refer to the following article:

    Windows7 elevated command prompt priviledges throug a script

    Thanks,
    Novak
    • Marked as answer by Novak Wu Thursday, March 25, 2010 2:07 AM
    Thursday, March 18, 2010 6:57 AM
  • Thank you Trent,

    But the problem is, I woulld not know which user will be running the script.
    I am thinking of sending it to a bunch of guys. The UAC prompt can check their permission and if they are admin, all they have to do is say OK.

    Thanks,
    Ray
    Thursday, March 18, 2010 8:07 PM
  • Hi Ray,

     

    As you mentioned, if the user profile has administrator privilege, the script will be run without any problem.

     

    However, if the user do not have administrator privilege, you should add some command in the batch file. For more information about elevating privilege, please refer to above article.

     

    Thanks,

    Novak

     

    • Proposed as answer by tkw829 Monday, July 25, 2016 12:47 PM
    Friday, March 19, 2010 2:47 AM
  • I need to do the same! Just to be clear: I don't know neither the current user name, nor the admin password. I just need to ask the user via UAC, and if he confirm, run the script ITSELF as if it as been opened via "Run as administrator" menu.

     

    Thanks,

     

     


    Gianluigi Zanettini - http://www.megalab.it
    Sunday, March 21, 2010 10:51 AM
  • Hi,

     

    Been searching for the same issue. How can I automatically input the administrator password without having to type it in from the keyboard? My administrator account has a password.

    Thursday, November 10, 2011 8:42 AM
  • I have the same problem.

    I tried different search terms to find a solution but I am running in circles as I am always redirected to the same articles which don't offer a solution (like this thread)

    I don't want that the batch user needs to know beforehand that the scripts needs to be run with admin rights. The user should be able to just double click the script.

    If the batch detects that it has no admin rights (which can be done easily by checking the %errorlevel% variable after a command like 'at > nul') I want the batch to ask for elevation before it proceeds.

    W.t.h. is there no easy way to do this?

    As a workaround I currently use an error message like "this script must be run as administrator" when I detect that I have no admin rights but this is not my preferred solution because it is not user friendly at all.

    Friday, August 2, 2013 5:33 PM
  • As I have not found any simple script so far, here's my two cents (derived from one of the URLs below):

    set ELEVATE_APP=Full command line without parameters for the app to run
    set ELEVATE_PARMS=The actual parameters for the app
    echo Set objShell = CreateObject("Shell.Application") >elevatedapp.vbs
    echo Set objWshShell = WScript.CreateObject("WScript.Shell") >>elevatedapp.vbs
    echo Set objWshProcessEnv = objWshShell.Environment("PROCESS") >>elevatedapp.vbs
    echo objShell.ShellExecute "%ELEVATE_APP%", "%ELEVATE_PARMS%", "", "runas" >>elevatedapp.vbs

    DEL elevatedapp.vbs

    Regards,

    Nika.

    Thursday, April 24, 2014 12:07 PM
  • @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"
        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"
    :-------------------------------------- 

    I found this script and it works brilliantly. copy it before your batch file commands

    Else cut your batch file in two. The one you want in 'admincmd' paste it in system32 and call it from your other half of batch file.

    • Proposed as answer by Grishko Saturday, July 4, 2015 8:05 AM
    Tuesday, September 23, 2014 9:23 AM
  • Also works in Windows 8 and +, thanks!
    Saturday, July 4, 2015 8:05 AM
  • One small suggestion: change line 13 like so:

    Current code:

     "%temp%\getadmin.vbs"

    Proposed code:

    cscript "%temp%\getadmin.vbs"

    ==

    This prevents the unfortunate case where sometimes the command is interpreted as "edit this file" instead of "run this file" (happens on one of my machines).

    Friday, April 1, 2016 12:14 AM
  • Final version http://bit.ly/2j8FI8k
    Saturday, January 21, 2017 8:37 PM
  • this works but because it is a vbs script that actually does the elevate the UAC prompt comes up.

    I need no prompt.

    My situation is that I have a few thousand videos to process and each need its own temporary share.

    I can create and remove the share from the command prompt but need admin to do it. so this script does not work for me.

    Friday, August 25, 2017 6:02 AM
  • @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"
        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"
    :-------------------------------------- 

    I found this script and it works brilliantly. copy it before your batch file commands

    Else cut your batch file in two. The one you want in 'admincmd' paste it in system32 and call it from your other half of batch file.


    Funcionou perfeitamente no meu código.

    Obrigado.
    Saturday, October 21, 2017 1:44 PM
  • @echo off
    icacls "%windir%\system32\config\system" >nul 2>&1
    if [%errorlevel%] neq [0] (
            powershell -c "Start-Process \"%0\" -Verb runAs"
            exit
    )
    echo Running as admin.
    pause



    Thursday, August 30, 2018 5:10 AM
  • @echo off
    icacls "%windir%\system32\config\system" >nul 2>&1
    if [%errorlevel%] neq [0] (
         cd /d "%~dp0"
         mshta "javascript:var shell=new ActiveXObject('shell.application');shell.ShellExecute('%~nx0','','','runas',1);close();"
         exit
    )
    echo Running as admin.
    pause



    Thursday, August 30, 2018 5:40 AM