Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Run as Administrator command name

11 May 2014   #1
John256145

Windows 7 Home Premium 64-bit
 
 
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 System SpecsSystem Spec
.

11 May 2014   #2
Brink
Microsoft MVP

64-bit Windows 10 build 10166
 
 

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 System SpecsSystem Spec
11 May 2014   #3
DavidE

Win 7 Pro x64 SP1, Win 7 Ult x86 SP1
 
 

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


11 May 2014   #4
John256145

Windows 7 Home Premium 64-bit
 
 

Quote   Quote: Originally Posted by Brink View Post
[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 System SpecsSystem Spec
11 May 2014   #5
Brink
Microsoft MVP

64-bit Windows 10 build 10166
 
 

I'm glad that'll work for you.
My System SpecsSystem Spec
12 May 2014   #6
andrew129260

Windows 7 Professional x64 Sp1
 
 

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 System SpecsSystem Spec
Reply

 Run as Administrator command name




Thread Tools



Our Sites

Site Links

About Us

Find 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 13:00.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App