INTRODUCTION

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.

THE SCENARIO

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.

SOLUTION

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.

powershell run as administrator

CONCLUSION

Always run Powershell windows elevated, and if you have mistakenly forgot about it, use my solution 🙂

USEFUL RESOURCES

BUY POWERSHELL BOOKS ON AMAZON

REFERENCES

RELATED POSTS