Last active
November 10, 2022 14:09
-
-
Save mcc85s/785e7c83d14bd57cb7a52a2eefc94f73 to your computer and use it in GitHub Desktop.
self elevating powershell script
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
$ID = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() | |
If (!($ID.IsInRole("Administrator") -or $ID.IsInRole("Administrators"))) | |
{ | |
If (Get-CimInstance Win32_OperatingSystem | ? { [UInt32]$_.BuildNumber -ge 6000 }) | |
{ | |
Write-Host "Not running as admin, attempting elevation..." | |
$Command = $MyInvocation | % { "-File `"{0} {1}`"; Exit" -f $_.MyCommand.Path, $_.UnboundArguments } | |
Start-Process PowerShell -Verb Runas -Args $Command | |
} | |
Else | |
{ | |
Throw "Must run as an administrator." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's so special or necessary about GCIM 6000, I tried and this works pretty good too(Windows 10/11) and it doesn't forget current directory: