How to Run a Program as a Different User with RUNAS
In a Windows environment, tasks are always executed under the security context of a specific user account. Sometimes, a script needs to run a program as a different user to access resources that the current user doesn't have permissions for, or to perform an administrative task that requires an elevated account. The standard, built-in command for this is runas.exe.
This guide will teach you how to use the runas command to launch a program as another user. You will learn the critical limitation of this command, i.e. its inability to accept a password directly, and the standard workarounds, including using /savecred or delegating to more powerful tools like PowerShell.
The Core Command: runas.exe
The runas command allows a user to run a specific tool or program with the permissions of another user account. This is a temporary, one-time elevation for a single process.
Syntax: runas /user:<UserName> "command"
/user:<UserName>: The user account you want to run the program as. This can be a local user (MY-PC\LocalAdmin), a domain user (MYDOMAIN\DomainAdmin), or a UPN (admin@mydomain.com)."command": The full command line, including the program and its arguments, that you want to execute.
Basic Example: Launching an Application as Another User
This is the most direct use of the runas command. When you execute it, it will interactively prompt you to enter the password for the specified user.
@ECHO OFF
ECHO --- Running Notepad as a different user ---
ECHO You will be prompted to enter the password for 'TestUser'.
ECHO.
runas /user:TestUser "notepad.exe"
Example of console interaction:
--- Running Notepad as a different user ---
You will be prompted to enter the password for 'TestUser'.
Enter the password for TestUser:
Attempting to start notepad.exe as user "MY-PC\TestUser" ...
After you type the password and press Enter, a new Notepad window will open, running under the security context of TestUser.
The Big Limitation: You Can't Provide a Password in a Script
For security reasons, runas.exe has no command-line switch to accept a password. You cannot pipe a password to it, redirect a password file, or provide it as an argument.
runas /user:TestUser /password:MyPass "notepad.exe" -> This is NOT a valid command.
This makes runas very difficult to use in a fully automated, non-interactive script. Any script using runas will halt and wait for a human to type a password.
Workaround 1: Using /savecred for Repeated Use
The /savecred switch tells runas to prompt for the password once, and then to save those credentials in the Windows Credential Manager for the current user. On subsequent runs of the same command, it will use the saved credentials automatically without prompting.
Syntax: runas /savecred /user:TestUser "command"
Critical Security Warning: Using /savecred is a significant security risk. It stores the password in a way that allows any other script or program running as your user to also use those credentials without a prompt. It should only be used in highly controlled environments on a secure machine.
Workaround 2 (Recommended): Using PowerShell for True Automation
For a truly automated script, you must use a more powerful tool that can handle credentials securely. PowerShell is the definitive built-in solution.
PowerShell can create a PSCredential object from a username and a "SecureString" (an encrypted password), and then pass this object to its Start-Process cmdlet.
The following script (Hybrid Batch and PowerShell) is a robust, non-interactive way to run a process as another user.
@ECHO OFF
SET "TargetUser=MY-PC\TestUser"
SET "TargetPass=S3cureP@ss!"
SET "CommandToRun=notepad.exe"
ECHO --- Launching process as '%TargetUser%' via PowerShell ---
powershell -Command "Start-Process -FilePath '%CommandToRun%' -Credential (New-Object System.Management.Automation.PSCredential('%TargetUser%', (ConvertTo-SecureString '%TargetPass%' -AsPlainText -Force)))"
ECHO Process launched.
This is the professional and recommended method for any fully automated script that needs to run a process as a different user.
Key runas Parameters Explained
/user:<UserName>: (Required) The user context to run as./noprofile: Speeds up the launch by not loading the user's full profile./env: Uses the current environment instead of the new user's./savecred: Caches the password after the first use. Use with extreme caution.
Common Pitfalls and How to Solve Them
-
"RUNAS ERROR: Unable to run - ... 1326: Logon failure: unknown user name or bad password.": This is the most common error. It means the password you typed was incorrect or the username was not found.
-
"740: The requested operation requires elevation": This means the program you are trying to launch (
notepad.exe,cmd.exe, etc.) requires administrator rights itself. If the user account you are using withrunasis not an administrator, the command will fail.- Solution: The user account specified in
/usermust have the necessary permissions to run the target application.
- Solution: The user account specified in
Conclusion
The runas.exe command is a useful tool for interactive, one-off tasks where a user can be prompted for a password.
- The core syntax is
runas /user:UserName "command". - It is fundamentally unsuitable for fully automated scripts because it cannot accept a password as an argument.
- The
/savecredswitch offers a workaround but comes with significant security risks. - For any truly automated or professional script, the recommended method is to delegate the task to a PowerShell one-liner using
Start-Processwith aPSCredentialobject.