47

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?

6 Answers 6

21

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.

42

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"
2
  • 2
    Interestingly, works in the old Powershell (5.1), but anymore in the new Powershell 6 (6.2.3).
    – Niko Fohr
    Nov 22, 2020 at 20:26
  • Unix-like sudo -i to run commands as admin in the same window does not exist, you need a new window in powershell to be admin.
    – Timo
    Nov 27, 2022 at 10:44
14

You can use this to self-elevate a script when ran:

#at top of script
if (!
    #current role
    (New-Object Security.Principal.WindowsPrincipal(
        [Security.Principal.WindowsIdentity]::GetCurrent()
    #is admin?
    )).IsInRole(
        [Security.Principal.WindowsBuiltInRole]::Administrator
    )
) {
    #elevate script and exit current non-elevated runtime
    Start-Process `
        -FilePath 'powershell' `
        -ArgumentList (
            #flatten to single array
            '-File', $MyInvocation.MyCommand.Source, $args `
            | %{ $_ }
        ) `
        -Verb RunAs
    exit
}

#example program, this will be ran as admin
$args
Pause

NB this still adheres to security rules (like execution policy) and will nicely prompt for UAC. This can be worked around, but you shouldn't.

1
12

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.

1
  • In Windows 11, this will ask if you want the command to make changes to your computer - so can't be used in a batch script.
    – iggy
    Apr 3, 2022 at 3:30
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".

3

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.

3
  • Any way to do this in Terminal and keep the new console in terminal? Apr 25, 2023 at 16:37
  • @DanielWilliams, no, there is no way to elevate the UAC of MS Terminal and retain the same window. Terminal is deisgned so that UAC change is always in a new window. May 21, 2023 at 16:44
  • refreshenv is just an alias for Update-SessionEnvironment, which is a component of chocolatey. May 21, 2023 at 16:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .