Execute a program under a different user account.
Syntax
RUNAS [/profile] [/env] [/netonly] /user:user Program
Key
/profile Option to load the user's profile (registry)
/env Use current environment instead of user's.
/netonly Use the credentials specified only for remote connections.
/user Username in form USER@DOMAIN or DOMAIN\USER
(USER@DOMAIN is not compatible with /netonly)
Program The command to execute
Enter the password when prompted.
When you start a program with RunAs /netonly, the program will execute on your local computer as the user you are currently logged on as, but any connections to other computers on the network will be made using the user account specified.
Without /netonly everything will run under the user account specified.
RunAs from Windows Explorer
Select an executable file, Shift-Right-click and select Run As..
This option can be hidden by setting
HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
HideRunAsVerb=1
ErrorLevel
The error level (%ERRORLEVEL%) returned by RunAs in Windows XP and above: success=0, failure=1
Run with Elevated permissions
In Windows Vista and above, you can run a script with elevated permissions by right clicking and choosing "Run As Administrator" but it’s also possible to invoke the UAC dialogue by calling the VBScript .ShellExecute function.
When a script is run with elevated permissions several aspects of the user environment may change: The current directory, the current TEMP folder and any mapped drives will be disconnected.
Examples
Run mycommand.exe as the user Jdoe on domain SS64dom
Runas /user:SS64Dom\jDoe "mycommand.exe"
Run CMD.exe as the Administrator on the local machine:
Runas /profile /user:DELLPC218\administrator CMD
Run Notepad.exe as the Domain Administrator on domain SS64dom
Runas /profile /env /user:SS64Dom\administrator NOTEPAD
Run Notepad.exe and open a file, escape the quote characters around the filename with \
Runas /env /user:jDoe@swest.ss64.com "NOTEPAD \"my file.txt\""
Notes:
The backslash \ is NOT the standard escape character used by other CMD commands.
RunAs Reqires the "Secondary Logon" service to be running.
“He who reigns within himself, and rules passions, desires, and fears, is more than a king” ~ Milton
Related:
ShellRunAs - Run a command under a different user account
Aaron Margosis - Running with least privilege
joeware.net - CPAU (Create Process
As User) like RunAs but with an options to encrypt the password.
PsExec - Execute process remotely
Powershell: you can run an entire powershell session via RunAs to elevate your permissions.
Equivalent bash command (Linux): SU - Switch User
© Copyright SS64.com 1999-2013
Some rights reserved