Windows Vista: How to execute a .exe program in the cmd "As Administrator"

11

2

Yes, that's it. I need to run a program .exe in the dos cmd line and it needs to run like when you right button and select "run as an administrator"

pribeiro

Posted 2008-09-29T21:31:21.490

Reputation:

I wondered about that too. My solution was either to make a batch file with the command and let it always run as admin, or to open the cmd as admin. But it feels like there should be some elegant solution. – OregonGhost – 2008-09-29T21:34:52.650

Answers

15

Windows has a "runas" command that acts a bit like sudo does in Unix-y systems. Try typing "runas /?" in your command prompt.

Neall

Posted 2008-09-29T21:31:21.490

Reputation: 281

Or see it here: http://technet.microsoft.com/en-us/library/cc771525.aspx

– Asaf R – 2008-09-29T21:51:17.103

This is not a correct answer, unless you have the Administrator user enabled with a non-empty password. – jiggunjer – 2016-02-02T04:44:59.617

7

If you run cmd itself as administrator, everything else you run from there will also. I just setup a shortcut to the command prompt that opens as administrator. Everything from there is good to go.

Kilhoffer

Posted 2008-09-29T21:31:21.490

Reputation: 171

5

I have used a duo of .cmd & .vbs script called elevate, it works fine for my needs. All you need is to type

elevate [command]
from Start > Run or from the command line, and it will run the command with administrator privileges. Hope it helps!

Here is elevate.cmd:

:: //***************************************************************************
:: // ***** Script Header *****
:: //
:: // File:      Elevate.cmd
:: //
:: // Additional files required:  Elevate.vbs
:: //
:: // Purpose:   To provide a command line method of launching applications that
:: //            prompt for elevation (Run as Administrator) on Windows Vista.
:: //
:: // Usage:     elevate.cmd application <application arguments>
:: //
:: // Version:   1.0.0
:: // Date :     01/02/2007
:: //
:: // History:
:: // 1.0.0   01/02/2007  Created initial version.
:: //
:: // ***** End Header *****
:: //***************************************************************************


@echo off

:: Pass raw command line agruments and first argument to Elevate.vbs
:: through environment variables.
set ELEVATE_CMDLINE=%*
set ELEVATE_APP=%1

start wscript //nologo "%~dpn0.vbs" %*

and elevate.vbs:

' //***************************************************************************
' // ***** Script Header *****
' //
' // File:      Elevate.vbs
' //
' // Additional files required:  Elevate.cmd
' //
' // Purpose:   To provide a command line method of launching applications that
' //            prompt for elevation (Run as Administrator) on Windows Vista.
' //
' // Usage:     (Not used directly.  Launched from Elevate.cmd.)
' //
' // Version:   1.0.1
' // Date :     01/03/2007
' //
' // History:
' // 1.0.0   01/02/2007  Created initial version.
' // 1.0.1   01/03/2007  Added detailed usage output.
' //
' // ***** End Header *****
' //***************************************************************************


Set objShell = CreateObject("Shell.Application")
Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objWshProcessEnv = objWshShell.Environment("PROCESS")

' Get raw command line agruments and first argument from Elevate.cmd passed
' in through environment variables.
strCommandLine = objWshProcessEnv("ELEVATE_CMDLINE")
strApplication = objWshProcessEnv("ELEVATE_APP")
strArguments = Right(strCommandLine, (Len(strCommandLine) - Len(strApplication)))

If (WScript.Arguments.Count >= 1) Then
    strFlag = WScript.Arguments(0)
    If (strFlag = "") OR (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
        OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "-?") OR (strFlag="h") _
        OR (strFlag = "?") Then
        DisplayUsage
        WScript.Quit
    Else
        objShell.ShellExecute strApplication, strArguments, "", "runas"
    End If
Else
    DisplayUsage
    WScript.Quit
End If


Sub DisplayUsage

    WScript.Echo "Elevate - Elevation Command Line Tool for Windows Vista" & vbCrLf & _
                 "" & vbCrLf & _
                 "Purpose:" & vbCrLf & _
                 "--------" & vbCrLf & _
                 "To launch applications that prompt for elevation (i.e. Run as Administrator)" & vbCrLf & _
                 "from the command line, a script, or the Run box." & vbCrLf & _
                 "" & vbCrLf & _
                 "Usage:   " & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate application <arguments>" & vbCrLf & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "Sample usage:" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate notepad ""C:\Windows\win.ini""" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate cmd /k cd ""C:\Program Files""" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate powershell -NoExit -Command Set-Location 'C:\Windows'" & vbCrLf & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "Usage with scripts: When using the elevate command with scripts such as" & vbCrLf & _
                 "Windows Script Host or Windows PowerShell scripts, you should specify" & vbCrLf & _
                 "the script host executable (i.e., wscript, cscript, powershell) as the " & vbCrLf & _
                 "application." & vbCrLf & _
                 "" & vbCrLf & _
                 "Sample usage with scripts:" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate wscript ""C:\windows\system32\slmgr.vbs"" –dli" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate powershell -NoExit -Command & 'C:\Temp\Test.ps1'" & vbCrLf & _
                 "" & vbCrLf & _
                 "" & vbCrLf & _
                 "The elevate command consists of the following files:" & vbCrLf & _
                 "" & vbCrLf & _
                 "    elevate.cmd" & vbCrLf & _
                 "    elevate.vbs" & vbCrLf

End Sub

kolrie

Posted 2008-09-29T21:31:21.490

Reputation: 191

Just curious, what is the advantage of this over runas? – tfinniga – 2008-09-29T22:07:02.947

Duh! You got me! :) I wouldn't know the advantage over runas, but when you don't know runas, that's the only alternative.

I am definitely embarrassed :) – None – 2008-09-29T23:08:42.440

I think it would help if we once read what we are using as a script! – None – 2008-09-29T23:09:25.777

1

This duo is descibed at http://technet.microsoft.com/en-us/magazine/2007.06.utilityspotlight.aspx

– None – 2009-02-11T11:27:18.590

2The clear advantage of the elevate script is that they enable you to elevate your user to administrator under UAC without a need to provide admin user and password (providing that you are logged in as an admin user). Can runas.exe do it? – None – 2009-02-11T11:28:26.200

3

You have to use the RunAs.exe command.

Here are some instructions: http://www.softtreetech.com/24x7/archive/53.htm

Gthompson83

Posted 2008-09-29T21:31:21.490

Reputation: 131

RunAs is incorrect. It does not elevate the command. – surfasb – 2011-12-23T11:59:04.430

2

You could also Press Windows Key, type CMD, this will list cmd in programs, right click it -> Properties -> Compatibility -> Run this program as an administrator

This will always run as Admin.

PostMan

Posted 2008-09-29T21:31:21.490

Reputation: 201

You can also use Ctrl+Shift+Enter. – Joey – 2009-06-08T22:25:32.807

1

Since someone posted the VBS equivalent, here's a Invoke-Admin function for PowerShell

function Invoke-Admin() {
param ( [string]$program = $(throw "Please specify a program" ),
        [string]$argumentString = "",
        [switch]$waitForExit )

$psi = new-object "Diagnostics.ProcessStartInfo"
$psi.FileName = $program 
$psi.Arguments = $argumentString
$psi.Verb = "runas"
$proc = [Diagnostics.Process]::Start($psi)
if ( $waitForExit ) {
    $proc.WaitForExit();
}

}

JaredPar

Posted 2008-09-29T21:31:21.490

Reputation: 1 041

0

In short:

  • Click/press Start, then CTRL + SHIFT and ENTER. Confirm.

In detail:

  1. Press the Start button Type "cmd" (without quotes) immediatly(1)
  2. Hold CTRL and SHIFT
  3. Press ENTER
  4. Confirm the Windows Vista UAC dialog box

(1) Immediatly means typing it in the Search box, not in the Run box.

Mackaaij

Posted 2008-09-29T21:31:21.490

Reputation: 101

0

You can use a VBScript as so. Create a .vbs file, e.g. ambika.vbs:

Set objShell = CreateObject(“Shell.Application”)
Set objWshShell = WScript.CreateObject(“WScript.Shell”)
Set objWshProcessEnv = objWshShell.Environment(“PROCESS”)

objShell.ShellExecute “C:\Windows\system32\cmd.exe”, “/k”, “”, “runas”

Ambika Singh

Posted 2008-09-29T21:31:21.490

Reputation: 1