how to run a powershell script as administrator

63

17

On my Windows 7 Desktop, I have script.ps1, which needs admin privileges (it starts a service). I want to click on this script and run it with admin privileges.

What's the easiest way to accomplish this?

Sajee

Posted 2010-02-12T21:46:32.473

Reputation: 3 899

Answers

53

Here is one way of doing it, with the help of an additional icon on your desktop. I guess you could move the script someone else if you wanted to only have a single icon on your desktop.

  1. Create a shortcut to your Powershell script on your desktop
  2. Right-click the shortcut and click Properties
  3. Click the Shortcut tab
  4. Click Advanced
  5. Select Run as Administrator

You can now run the script elevated by simple double-clicking the new shortcut on your desktop.

Kez

Posted 2010-02-12T21:46:32.473

Reputation: 15 359

@mousio can you tell me why that command works? – SShaheen – 2014-08-22T12:54:47.250

7@SShaheen - for Run as Administrator to become available, the shortcut needs to point to some sort of executable (e.g. powershell.exe) instead of just the document or script the shortcut originally pointed to. A shortcut to script.ps1 works, as does a shortcut to powershell.exe -f script.ps1, but the latter can be set to run as administrator (see powershell.exe /? for the explanation of the -f or -File switch) – mousio – 2014-08-22T13:30:00.387

40This worked for me, but Run as Administrator only became available after adding powershell -f in front of the script path, so as to "complete" the command… – mousio – 2012-11-01T22:40:05.850

2@mousio - I needed this too, thanks for the comment – m.edmondson – 2013-03-18T17:15:17.517

20

On UAC-enabled systems, to make sure a script is running with full admin privileges, add this code at the beginning of your script:

param([switch]$Elevated)

function Test-Admin {
  $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
  $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

if ((Test-Admin) -eq $false)  {
    if ($elevated) 
    {
        # tried to elevate, did not work, aborting
    } 
    else {
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}

exit
}

'running with full privileges'

when running your script with the -elevated switch, it will attempt to elevate privileges before running.

MDMoore313

Posted 2010-02-12T21:46:32.473

Reputation: 4 874

If script requires arguments-parameters ? – Kiquenet – 2014-08-29T13:50:41.370

what if this tells me it is running with full privileges, but my code still says insufficient administrative privileges? – mike.b93 – 2017-03-24T18:12:30.907

1@Kiquenet add it into the param(...) on top and forward them right before -elevated, you'll need to be clever about how you build the ArgumentList, probably will want to use the String[] form. – TWiStErRob – 2019-01-11T21:47:44.763

pretty nifty - better than creating a shortcut – Mikey – 2019-02-06T20:01:33.187

13

if you are in the same powershell you could do this:

Start-Process powershell -verb runas -ArgumentList "-file fullpathofthescript"

mjsr

Posted 2010-02-12T21:46:32.473

Reputation: 5 577

The problem with this is that it changes the working directory for the called script to C:\Windows\System32. An alternative which preserves the current directory: https://stackoverflow.com/a/57033941/2441655

– Venryx – 2019-07-15T06:35:23.020

4

Since it's sitting onto your desktop, I'd say the most effortless way to get this done is dragging it onto the elevation gadget.

Otherwise you could make a separate script using the elevate command on your ps1 script.

Or, you could apply elevate just to the service-starting bit.

badp

Posted 2010-02-12T21:46:32.473

Reputation: 3 457

1

PowerShell ISE lives at %windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe. You can right-click that and select "Run as administrator" and run the script from in there.

You can also find it under the Windows Logo > All Programs > Accessories > Windows PowerShell and do the same thing with those shortcuts.

vapcguy

Posted 2010-02-12T21:46:32.473

Reputation: 111

0

If you want an option to launch a Powershell script as adminstrator, directly from the Explorer context-menu, see section 2 of my answer here: https://stackoverflow.com/a/57033941/2441655

Venryx

Posted 2010-02-12T21:46:32.473

Reputation: 121

-1

Add this to the beginning of the script:

$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if ($testadmin -eq $false) {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
exit $LASTEXITCODE
}

Anthony

Posted 2010-02-12T21:46:32.473

Reputation: 9

This seems to be a subset of MDMoore313’s answer, from over four years ago.

– Scott – 2017-10-23T21:04:39.460