Skip to content

Instantly share code, notes, and snippets.

@mitch-b
Last active September 24, 2019 14:54
Show Gist options
  • Save mitch-b/db5faaf2ff50fa0ca979d741dcce2c45 to your computer and use it in GitHub Desktop.
Save mitch-b/db5faaf2ff50fa0ca979d741dcce2c45 to your computer and use it in GitHub Desktop.
Self-Elevating PowerShell Script Snippet
# 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
}
}
# TODO stuff
# ...
Write-Host "Press any key to continue ..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment