Last active
September 24, 2019 14:54
-
-
Save mitch-b/db5faaf2ff50fa0ca979d741dcce2c45 to your computer and use it in GitHub Desktop.
Self-Elevating PowerShell Script Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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