Run PowerShell as Another User

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 runas command.

Open the PowerShell as an Administrator

To open the PowerShell as an Administrator:

  • Press the Windows key + R to open the Run window.
  • Type powershell and press Ctrl + Shift + Enter to open PowerShell as an administrator.

    run powershell as another user - open powershell

Run the runas command

In the PowerShell window, use the runas command to open the PowerShell.

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.

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-Process cmdlet.
  • Close the parent PowerShell window.

Run the Start-Process cmdlet

In the PowerShell window, use the Start-Process command to open the PowerShell.

The above command will prompt the user to enter the username and password for the user account.

run powershell as another user - enter username and password

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 credential object.
  • Run the Start-Process cmdlet.

Declare the username and password

In the PowerShell window, declare the username and password through the following commands.

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:

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:

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.

Was this post helpful?

Related Posts

  • Convert Month name to number in PowerShell
    23 January

    Convert Month Number to Name in PowerShell

    Table of ContentsUsing the Get-Culture() CmdletConvert the Hardcoded Month Number to the NameConvert the Month Number from a Date to the NameConvert the Current Month Number to the NameUsing the [cultureinfo]::InvariantCulture Property Using the Get-Culture() Cmdlet We can use the Get-Culture cmdlet in the following three possible ways: To Convert the Hardcoded Month Number to […]

  • Delete empty folders in PowerShell
    23 January

    Delete Empty Folders in PowerShell

    Table of ContentsUsing the Remove-Item with Get-ChildItem CmdletUsing the Get-ChildItem and ForEach-ObjectUsing the User-defined Function There are multiple ways that we can use to delete the empty folders in PowerShell but before going into the details, let’s have a look at the following directory structure to know how many and which folders are empty. [crayon-63d27df73ca86820538551/] […]

  • PowerShell- Where-Object with multiple conditions
    23 January

    PowerShell – Where-Object with Multiple Conditions

    Table of ContentsUsing Where-Object Cmdlet with Multiple ConditionsScript BlockComparison Statements Using Where-Object Cmdlet with Multiple Conditions In PowerShell, we have various comparison and logical operators that we can use with the Where-Object cmdlet to apply multiple conditions. Let’s explore a few scenarios below. [crayon-63d27df73d019557423881/] [crayon-63d27df73d01e731388619/] The above command returned an item where the extension of […]

  • Rename Files with PowerShell
    18 January

    Rename Files with PowerShell

    Table of ContentsRename Single File with PowerShellUsing Rename-Item with/without the -Path ParameterUsing Rename-Item with -Confirm ParameterUsing Rename-Item with -Passthru ParameterRename multiple files in PowerShell Rename Single File with PowerShell Using Rename-Item with/without the -Path Parameter Use the Rename-Item cmdlet to rename a file in PowerShell. [crayon-63d27df73d57c567592589/] We used the Rename-Item cmdlet to rename the test.txt […]

  • PowerShell - redirect output to File
    18 January

    PowerShell – Redirect Output to File

    Table of ContentsUsing Out-File CmdletUsing Redirection OperatorsUsing StreamWriter .Net ClassUsing Add-Content CmdletUsing Set-Content Cmdlet Using Out-File Cmdlet Use the Out-File cmdlet to redirect PowerShell output to a file. [crayon-63d27df73da5a690740484/] The Get-Process is used to retrieve all the processes that are currently running on our machine (local computer). We can filter this information by specifying parameters; […]

  • PowerShell - Logical operators
    18 January

    PowerShell – Logical Operators

    Table of ContentsUsing Logical Operators in PowerShellUse Logical AND OperatorUse Logical OR OperatorUse Logical EXCLUSIVE OR OperatorUse Logical NOT OperatorUse Multiple Logical Operators Using Logical Operators in PowerShell PowerShell’s logical operator is a potent tool for comparing data and carrying out operations depending on those comparisons. It allows the user to design an if-then scenario […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.