29

I only have one account on my Windows Vista machine, and that user has administrative privileges. However, when I try to execute a command within PowerShell to kill a certain process, I'm greeted with an "Access is denied" message. How do I become the administrator?

14

The easiest way to do this is to launch Powershell with administration tokens. To do this, you right click on Powershell (or a shortcut to it) and click on "run as administrator". Alternatively you can use elevate.cmd.

| improve this answer | |
28

The Powershell v2 way, according to Microsoft, is to right click on the shortcut and choose Run as Administrator.

And to elevate within a Powershell window:

start-process powershell –verb runAs

Which from a cmd.exe batch file, shortcut or Run line would look something (repetitively) like this:

powershell "start-process powershell -verb runas"
| improve this answer | |
10

This opens a new powershell instance:

function Run-Elevated ($scriptblock)
{
  # TODO: make -NoExit a parameter
  # TODO: just open PS (no -Command parameter) if $scriptblock -eq ''
  $sh = new-object -com 'Shell.Application'
  $sh.ShellExecute('powershell', "-NoExit -Command $scriptblock", '', 'runas')
}

I expect that there are issues with this - in particular, you won't get the output of your scriptblock back in the calling script. On the other hand, it will be there in the new PS instance so you can hack with it there.

| improve this answer | |
5

If you want to always run PowerShell with admin priveleges, you can right-click the PowerShell shortcut, then click the "Advanced..." button on the "Shortcut" tab, then select "Run as Administrator".

| improve this answer | |
1

The easy way...

This is how to set up an easy command for getting admin privileges anytime, from any PowerShell session!

Step 1: Open your PowerShell Profile. (Allow notepad to create the file if it's missing.)

notepad "$profile"

Step 2: Paste one of the following lines of code anywhere in that file. Choose a command below based on the PowerShell version you want to run, and note that the profile is version-specific, so you have to do this for each of your different PowerShell version profiles if you run them in parallel.

# PowerShell 5 (old version built into windows)
function GoAdmin { Start-Process powershell –Verb RunAs }

# PowerShell Core (the latest PowerShell version from GitHub)
function GoAdmin { Start-Process pwsh –Verb RunAs }

Step 3: Restart PowerShell. The command is now permanently available.

Step 4: Anytime you want to go admin, simply type GoAdmin (and don't worry about typing the correct case; PS functions are actually case insensitive). You can of course name the function something else like Elevate or whatever, but I didn't want any risk of conflicts so I named it a two-word phrase without any hyphens, to ensure it'll never conflict with PowerShell's own features.

Note that the administrator window opens in a separate shell. You can close the original shell, or keep it open. If your admin work does something "big" such as adding/modifying environment variables, then your user-shell (non-admin) can simply type refreshenv to reload its environment and get the changes. (Edit: Discovered that refreshenv is provided by the great Chocolatey package manager. Type Update-SessionEnvironment instead if you don't have Chocolatey.)

So basically, just GoAdmin, do the admin thingies such as installing packages (with https://chocolatey.org/ of course!), and then close the admin window and run refreshenv in your user-shell! Voila.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.