Table of Contents
Using the runas Command
The runas command in PowerShell launches programs using credentials different from the current user. It provides users with limited privileges to execute commands or access resources available only to more privileged accounts.
The command’s syntax is runas /user:UserName ProgramName where UserName is an account with permission and ProgramName is the name of the application, script, document, etc., that can be opened or run by this account.
To run PowerShell as another user using the runas command, we must meet the requirements below.
- Open the PowerShell as an
Administrator. - Then, Run the
runascommand.
Open the PowerShell as an Administrator
To open the PowerShell as an Administrator:
- Press the Windows key + R to open the Run window.
-
Type
powershelland press Ctrl + Shift + Enter to open PowerShell as an administrator.
Run the runas command
In the PowerShell window, use the runas command to open the PowerShell.
|
1 2 3 |
runas /user:UserName "powershell.exe" |
Replace the UserName with the name of the desired account. The above command will prompt the user to enter the password for the user account. After entering the correct password, a new PowerShell window will open, running under the specified user account.
After successfully opening the PowerShell window, use the whoami command in the PowerShell window to check if the window is running under the specified user account.
|
1 2 3 |
whoami |
Further reading:
Using the Start-Process cmdlet with the Credential Parameter
Start-Process is a cmdlet that starts one or more processes on the local computer. In contrast, the Credential parameter allows the user to provide alternate credentials to the processes that require authentication.
UI-Based Solution
The solution is to provide the credentials through a UI dialog box. We discussed the opening of the PowerShell as an Administrator in the explanation of using the runas command. In this section, we must meet the below-given requirements.
- Run the
Start-Processcmdlet. - Close the parent PowerShell window.
Run the Start-Process cmdlet
In the PowerShell window, use the Start-Process command to open the PowerShell.
|
1 2 3 |
Start-Process powershell.exe -Credential '' |
The above command will prompt the user to enter the username and password for the user account.

After entering the correct password, a new PowerShell window will open, running under the specified user account.
Close the parent PowerShell window
By default, the Windows operating system does not let us type anything into a newly opened PowerShell window until we close the parent PowerShell window. So shut the parent window and use the whoami command for the verification process.
Script-Based Solution
The solution is to provide the command line with the user account credentials using script variables. To do this, we must meet the below-given requirements.
- Declare the username and password.
- Generate the
credentialobject. - Run the
Start-Processcmdlet.
Declare the username and password
In the PowerShell window, declare the username and password through the following commands.
|
1 2 3 4 5 |
$username = 'UserName' $password = 'Password' $securePassword = ConvertTo-SecureString $password -AsPlainText -Force |
Replace the username and password with those of the user’s account.
We used the ConvertTo-SecureString cmdlet to convert the password to a securePassword. The ConvertTo-SecureString cmdlet (command-line tool) converts a string or a plain text password into a secure string, i.e., an encrypted representation to store sensitive information, such as passwords or other sensitive data.
Generate the credential object
Generate the credential object using the below command:
|
1 2 3 |
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword |
We used the New-Object cmdlet to create a new instance credential of type System.Management.Automation.PSCredential with the username and securePassword as arguments. It is often used in scripts and other automated processes to create objects of a specific type and manipulate them as needed.
Run the Start-Process cmdlet
To start a new instance of the PowerShell with a different user, use the Start-Process cmdlet as:
|
1 2 3 |
Start-Process powershell.exe -Credential $credential |
Using the Start-Process cmdlet, we started a new instance of PowerShell where the Credential parameter is provided with the generated credential object.
It will open the new PowerShell window. Next, close the previous window and use the whoami command on the new window to verify if the PowerShell window is running under the new user.