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"
10 Answers
Windows has a "runas" command that acts a bit like sudo does in Unix-y systems. Try typing "runas /?" in your command prompt.
-
-
This is not a correct answer, unless you have the Administrator user enabled with a non-empty password. Feb 2, 2016 at 4:44
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.
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
-
-
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 :)– kolrieSep 29, 2008 at 23:08
-
I think it would help if we once read what we are using as a script!– kolrieSep 29, 2008 at 23:09
-
1
-
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?– AnonymousFeb 11, 2009 at 11:28
You have to use the RunAs.exe command.
Here are some instructions: http://www.softtreetech.com/24x7/archive/53.htm
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.
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();
}
}
In short:
- Click/press Start, then CTRL + SHIFT and ENTER. Confirm.
In detail:
- Press the Start button Type "cmd" (without quotes) immediatly(1)
- Hold CTRL and SHIFT
- Press ENTER
- Confirm the Windows Vista UAC dialog box
(1) Immediatly means typing it in the Search box, not in the Run box.
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”
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.
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
)
::****************************************************************