Run Powershell Commands as an Administrator
This blog post describes how in powershell elevate to admin within an opened Window. It can be useful if you have already opened a Powershell window and have forgotten to elevate and want to run Powershell as administrator without closing the shell.
You have opened a Powershell window and the command that you are trying to run does not work. Of course, the issue is that you have not started an elevated window using Run as Administrator.
So, will I need to close my existing window and re-open? This is usually not a tedious task, but it might be if you need to input a complex password for an elevated account.
Fortunately, there is a way of solving this with a simple command from the non-elevated Powershell window. This blog post will show how to elevate to administrator within a Powershell window.
WHICH ACTIONS REQUIRE ELEVATION?
There are some actions in Powershell which require you to elevate, such as:
- Changing of the Execution Policy
- Modifications to System Files
- Modifications of the Registry
I would say that it is best practice to always start an elevated Powershell window, in order to not run in to any unexpected issues.
The only time I would not recommend this if you are testing a script that will run as a regular user.
It is also possible to add a snippet in a script to check if the current session is running in elevated mode. I will write more about this in an upcoming blog post.
Run the following command from a non-elevated Powershell prompt:
Start-Process Powershell -Verb runAs
If prompted by the UAC, enter the administrative credentials.
You will now have two windows, where one window is elevated.
Always run Powershell windows elevated, and if you have mistakenly forgot about it, use my solution 🙂
BUY POWERSHELL BOOKS ON AMAZON
- Microsoft Docs – Powershell Documentation
- Microsoft Docs – User Account Control (Windows applications)
- Use manage-bde -status to check Bitlocker encryption status on a Windows 10 client
- Powershell script to add Active Directory users to groups
- Powershell script to add Active Directory groups to local administrator group
- Add user to local administrator group using Powershell
About the author
Daniel Classon works as a Senior Consultant at Mansoft, focusing on Microsoft Configuration Manager, Windows 10 and Powershell