Skip to content

Instantly share code, notes, and snippets.

@MagicAndi
Created May 13, 2015 13:15
Show Gist options
  • Save MagicAndi/7d8a680601bbf8ac85e0 to your computer and use it in GitHub Desktop.
Save MagicAndi/7d8a680601bbf8ac85e0 to your computer and use it in GitHub Desktop.
PowerShell snippet to run PowerShell code in an elevated (UAC) process. Taken from answers to this StackOverflow question: http://stackoverflow.com/questions/7690994/powershell-running-a-command-as-administrator
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
#"No Administrative rights, it will display a popup window asking user for Admin rights"
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments
break
}
#"After user clicked Yes on the popup, your file will be reopened with Admin rights"
#"Put your code here"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment