Run as Administrator command name


  1. Posts : 8
    Windows 7 Home Premium 64-bit
       #1

    Run as Administrator command name


    Is there a command to insert into a batch file so the file can ask for administrator approval before continuing?
      My Computer


  2. Posts : 71,977
    64-bit Windows 11 Pro for Workstations
       #2

    Hello John,

    I'm not sure about with a .bat file, but you could use this below with a .vbs file to have it prompt with UAC for elevation approval.

    Code:
    If WScript.Arguments.Count = 0 Then
     Set objShell = CreateObject("Shell.Application")
     objShell.ShellExecute "wscript.exe", Chr(34) & WScript.ScriptFullName & Chr(34) & " Run", , "runas", 1
    Else
     
    Your code here
     
    End If
    Hope this helps, :)
    Shawn
      My Computer


  3. Posts : 6,330
    Multi-Boot W7_Pro_x64 W8.1_Pro_x64 W10_Pro_x64 +Linux_VMs +Chromium_VM
       #3

    You can also create a Shortcut with Run as administrator to run the .bat file.
      My Computer


  4. Posts : 8
    Windows 7 Home Premium 64-bit
    Thread Starter
       #4

    Brink said:
    [CODE]If WScript.Arguments.Count = 0 Then
    Set objShell = CreateObject("Shell.Application")
    objShell.ShellExecute "wscript.exe", Chr(34) & WScript.ScriptFullName & Chr(34) & " Run", , "runas", 1
    Else
    Close Enough.:)
      My Computer


  5. Posts : 71,977
    64-bit Windows 11 Pro for Workstations
       #5

    I'm glad that'll work for you. :)
      My Computer


  6. Posts : 4,566
    Windows 10 Pro
       #6

    Yes there is a very easy way to have UAC ask for a batch file to prompt to run as admin:

    See this code:

    Code:
    :: 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"
    
    :--------------------------------------
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 11:46.
Find Us