Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
+1 for the question –  r0ca Feb 12 '10 at 22:14

4 Answers 4

up vote 16 down vote accepted

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.

share|improve this answer
9  
This 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 Nov 1 '12 at 22:40
2  
@mousio - I needed this too, thanks for the comment –  m.edmondson Mar 18 '13 at 17:15
    
@mousio can you tell me why that command works? –  SShaheen Aug 22 at 12:54
1  
@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 Aug 22 at 13:30

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

Start-Process powershell -verb runas -ArgumentList "-file fullpathofthescript"
share|improve this answer

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.

share|improve this answer
    
I'm not a fan of PS but I need to get into it... Good answer badp! –  r0ca Feb 12 '10 at 22:13

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.

share|improve this answer
    
If script requires arguments-parameters ? –  Kiquenet Aug 29 at 13:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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