13

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"

1
  • 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. Sep 29, 2008 at 21:34

10 Answers 10

15

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

2
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.

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
5
  • Just curious, what is the advantage of this over runas?
    – tfinniga
    Sep 29, 2008 at 22:07
  • 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 :)
    – kolrie
    Sep 29, 2008 at 23:08
  • I think it would help if we once read what we are using as a script!
    – kolrie
    Sep 29, 2008 at 23:09
  • 1
    This duo is descibed at technet.microsoft.com/en-us/magazine/…
    – Anonymous
    Feb 11, 2009 at 11:27
  • 2
    The 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?
    – Anonymous
    Feb 11, 2009 at 11:28
3

You have to use the RunAs.exe command.

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

1
  • RunAs is incorrect. It does not elevate the command.
    – surfasb
    Dec 23, 2011 at 11:59
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.

1
  • You can also use Ctrl+Shift+Enter.
    – Joey
    Jun 8, 2009 at 22:25
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();
}

}

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.

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”
0

A batch/JScript hybrid:

goto="Batch" /*
:Admin
REM CONTINUE YOUR CODE HERE
exit /b 0
:RunAsAdmin
color 4f & echo Running Admin Shell, Please wait.... & call cscript //nologo /e:JScript "%~f0" RunAsAdmin "%1" & exit /b
:"Batch"
@echo off & setlocal enabledelayedexpansion & cls
openfiles >nul 2>&1 || goto :RunAsAdmin
goto :Admin
:"JScript" */
function RunAsAdmin(self, arguments) { WSH.CreateObject('Shell.Application').ShellExecute(self, arguments, '', 'runas', 1) }
if (WSH.Arguments.length>=1 && WSH.Arguments(0)=='RunAsAdmin') RunAsAdmin(WSH.ScriptFullName,WSH.Arguments(1));
//

Continue your code in :Admin label. You can modify other text to display in :RunAsAdmin label.

0

I will show another example with Temporary VBScript:

@echo off
::****************************************************************
openfiles >nul 2>&1 & if %errorlevel% EQU 1 (
  setlocal disableDelayedExpansion & color 4f
  echo Running Admin Shell, Please wait.... & ping localhost -n 1 >nul
  set "bPath=%~f0" & setlocal enableDelayedExpansion 
  echo Dim UAC : Set UAC = CreateObject^("Shell.Application"^) >Elevate.vbs
  echo For Each strArg in Wscript.Arguments >>Elevate.vbs
  echo   args = args ^& strArg ^& " " >>Elevate.vbs
  echo Next >>Elevate.vbs
  echo UAC.ShellExecute "!bPath!",args,"","runas",1 >>Elevate.vbs
  echo Set objShell = Nothing >>Elevate.vbs
  cscript //nologo Elevate.vbs & del Elevate.vbs & exit /b
)
::****************************************************************

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.