Skip to content

Instantly share code, notes, and snippets.

@WindyNova
Created April 29, 2023 08:56
Show Gist options
  • Select an option

  • Save WindyNova/f063038f7d41529bf4a33c03bb67cfb8 to your computer and use it in GitHub Desktop.

Select an option

Save WindyNova/f063038f7d41529bf4a33c03bb67cfb8 to your computer and use it in GitHub Desktop.
PowerShell Admin Rights Self-Elevation Template
# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
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