Skip to content

Instantly share code, notes, and snippets.

@yoav-lavi
Created April 30, 2018 08:36
Show Gist options
  • Save yoav-lavi/463031d18348f5edac7867538bf2b50c to your computer and use it in GitHub Desktop.
Save yoav-lavi/463031d18348f5edac7867538bf2b50c to your computer and use it in GitHub Desktop.
Elevation function for PowerShell scripts
function Elevate {
Param ($Path)
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator') `
-And [int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000)
{
$CommandLine = "-File `"$Path`""
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment