Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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"

share|improve this question

migrated from stackoverflow.com Feb 8 '10 at 9:42

This question came from our site for professional and enthusiast programmers.

    
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 Sep 29 '08 at 21:34
    
There is. It's called runas :) –  Јοеу Jun 8 '09 at 22:26
add comment

9 Answers

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

share|improve this answer
    
Or see it here: technet.microsoft.com/en-us/library/cc771525.aspx –  Asaf R Sep 29 '08 at 21:51
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
    
Just curious, what is the advantage of this over runas? –  tfinniga Sep 29 '08 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 '08 at 23:08
    
I think it would help if we once read what we are using as a script! –  kolrie Sep 29 '08 at 23:09
1  
This duo is descibed at technet.microsoft.com/en-us/magazine/… –  Anonymous Feb 11 '09 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 '09 at 11:28
add comment

You have to use the RunAs.exe command.

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

share|improve this answer
    
RunAs is incorrect. It does not elevate the command. –  surfasb Dec 23 '11 at 11:59
add comment

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.

share|improve this answer
    
You can also use Ctrl+Shift+Enter. –  Јοеу Jun 8 '09 at 22:25
add comment

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();
}

}

share|improve this answer
add comment

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.

share|improve this answer
add comment

I created a link from %SystemRoot%\system32\cmd.exe in the documents folder called cmd.lnk and then I clicked properties, advanced, then clicked always run as administrator or whatever. Then I removed the file %SystemRoot%\system32\cmd.exe from the index by right clicking on the cmd.exe program, going to properties, clicking advanced, and then unchecking Index this file for faster searching. I went to the start menu and typed indexing options and I am now rebuilding the index. It is going to take a while but I am almost positive that once the rebuild is finished, when I type cmd from now on, it will find the cmd link in the my documents folder and run that with administrative privileges and not show the cmd.exe from the system32 folder. Hope this helps.

Spork

share|improve this answer
add comment

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”
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.