PowerShell Run as Different User
CredentialCommand to Run PowerShell as a Different User to Execute a Script
- UI Based Solution to Run PowerShell as a Different User to Execute a Stored Script
PowerShell is the Command line-based Automation and scripting solution to Windows, Mac, and Linux users. Many Operating System users can use PowerShell. One of the primary users who are using PowerShell is Windows users.
Windows have limited scripting capabilities, unlike in Linux and Linux-like Systems. Windows PowerShell is one good solution to fill this gap and provide Windows users with certain flexibility on CI/CD system management, scripting, and automation tasks.
More details about Windows and other versions of PowerShell can be found via https://docs.microsoft.com/en-us/powershell/, which includes official PowerShell documentation.
In Windows Systems, different users can log in through other credentials and use the same Windows system. In PowerShell, a user can log in as a different user (to which the user has access rights) and execute a script.
Credential Command to Run PowerShell as a Different User to Execute a Script
There are ways to change the user account by using the PowerShell application. First, you should open the PowerShell application. Then, the
credential keyword can be used to open the popup. Then you can enter your required user credentials (Username and Password).
start powershell -credential ""
As a result of the above command, the popup shown in the below image is seen, where you can enter your other user credentials to switch to a required user. The popup opened is the Windows PowerShell credential request.
However, instead of including the credential command only, you can use other methods for this purpose.
However, there is one thing to keep in mind here. If you use the newly opened PowerShell with the keyboard and other input devices, you must first close the parent PowerShell application window.
That is the window you used to open a new PowerShell Window as a different user. If not, you will face running commands as the new user.
Here the process is secured as it does not require you to type the Username and password texts in the input. However, there is a less secured approach to skip the above popup.
That method would provide the
$password and give the credential keyword along with
new-object to log in directly to the different user account you need to use PowerShell. It is not recommended to use due to potential security risks to the user account.
UI Based Solution to Run PowerShell as a Different User to Execute a Stored Script
Rather than using a complex process of running PowerShell as one user, providing commands, and logging in as a new user, there are more accessible UI-based solutions to achieve this.
One of the most recommended and easy ways is to first pin the PowerShell application to the taskbar of the Windows Machine.
Then you can right-click on the PowerShell icon, which shows you an option as Windows PowerShell. Hovering over that options, Click Shift and right-click together to open another menu.
You can choose
Run as Different User from the new menu. Then a different popup would be opened, as shown in the below image. Before starting the application, you can enter the User Credentials (Password and Username) of the user you intend to log in.
Thereby, the user can log in as a new user to an account to which they have access and use the PowerShell application as that particular user to run any stored script or any script of likeness.
However, unless you are logged in as an Admin, you may be restricted from performing some of the commands available through PowerShell. For example, any commands that would require any changes in System32 files would require full admin rights before executing the command. You can read more about elevated PowerShell sessions and commands requiring admin permission through https://community.idera.com/database-tools/powershell/ask_the_experts/f/learn_powershell_from_don_jones-24/21279/which-commands-require-running-as-an-admin.
Related Article - PowerShell User
- Check if User Exists in PowerShell
- Switch User Using PowerShell
- List Local Users and Groups Using PowerShell
- PowerShell Get Current User