Skip to content

Instantly share code, notes, and snippets.

@theTonyHo
Created July 3, 2020 09:34
Show Gist options
  • Save theTonyHo/fac6eccf2fcfb8118b0533db3ed2f3fe to your computer and use it in GitHub Desktop.
Save theTonyHo/fac6eccf2fcfb8118b0533db3ed2f3fe to your computer and use it in GitHub Desktop.
# Reference: https://ss64.com/ps/syntax-elevate.html
# Place this in beginning of script
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
# Relaunch as an elevated process:
Write-Host "Executing Script as Administrator" -ForegroundColor Green
Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path) -Verb RunAs
exit
}
Write-Host "Executing script"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment