Scenario / Questions

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?

Find below all possible solutions or suggestions for the above questions..

Suggestion: 1

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.

Suggestion: 2

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"

Suggestion: 3

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.

Suggestion: 4

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”.

Suggestion: 5

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 the following code anywhere in that file.

function GoAdmin { start-process powershell –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.

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.