11

Is there a way to run/start cmd as administrator through the command line or a batch file programming in Windows 8?

I want to create a batch file which has administrative privileges without any prompt to the user.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Ezra09
  • 111
  • 1
  • 1
  • 4
  • 1
    Have a look at the SU question: [On Windows 7, is there a command prompt line that can start cmd as an administrator?](http://superuser.com/questions/74564/on-windows-7-is-there-a-command-prompt-line-that-can-start-cmd-as-an-administra). As far as I remember you cannot bypas any UAC popup without disabling UAC completely – nixda Dec 30 '13 at 08:59
  • Related: *[On Windows 7, is there a command prompt line that can start cmd as an administrator?](http://superuser.com/questions/74564)* – Peter Mortensen Jul 21 '15 at 10:15

4 Answers4

5
runas /profile /user:administrator “Driver:\folder\program”

For example, the administrator account is “AAA” and you want to run BBB.exe of C:\programs, you should follow these steps:

  1. Press Win key & R
  2. Input “CMD” in open box and click “OK”
  3. Input: runas /profile /user:AAA “C:\programs\BBB.exe” and press “Enter”
  4. Input the password of administrator AAA
  5. Press “Enter”

Hope it works.

Brad Turek
  • 117
  • 6
Unnikrishnan
  • 1,307
  • 2
  • 12
  • 24
1

You can use runas.exe /savecred /user:administrator cmdor refer this link

Renju Chandran chingath
  • 1,465
  • 3
  • 12
  • 19
0
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"  
phuclv
  • 26,555
  • 15
  • 113
  • 235
  • 1
    You should put some additional content in this answer explaining what it is doing rather than just proving some back code mixed with VBS and no explanation leaving people that don't know this code scratching their head wondering what it does. I'd look it up and test with it regardless if it was me using it and not understanding it but you get the point. – Vomit IT - Chunky Mess Style Jul 18 '17 at 05:13
0

You can download already created portable and clean (generated in the Windows XP) shortcut files set: https://sourceforge.net/p/contools/contools/HEAD/tree/trunk/Scripts/Tools/ToolAdaptors/lnk/

Or even generate your own: https://sourceforge.net/p/contools/contools/HEAD/tree/trunk/Scripts/Tools/ToolAdaptors/vbs/

Usage example:

cmd_admin.lnk /C ...

Each lnk file just a link to the cmd.exe, so you can pass here all the cmd.exe command line options.

Pros:

  • You don't need a localized version of Administrator account name like for the runas method.

Cons:

  • You start elevated only the cmd.exe process. To start any other process you have to either run it from the cmd.exe script, or create another standalone shortcut with the Run as Administrator flag raised.
  • Run from shortcut file (.lnk) in the Windows XP (but not in the Windows 7) brings truncated command line down to ~260 characters.
  • Run from shortcut file (.lnk) loads console windows parameters (font, windows size, buffer size, etc) from the shortcut at first and from the registry (HKCU\Console) at second. If try to change and save parameters, then it will be saved ONLY into the shortcut, which brings the shortcut file overwrite.
Andry
  • 101
  • 4