Last active
March 19, 2025 09:18
-
-
Save mcc85s/785e7c83d14bd57cb7a52a2eefc94f73 to your computer and use it in GitHub Desktop.
self elevating powershell script
This file contains hidden or 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
With the help of ChatGPT, after numerous attempts, I have managed to write the shortest code for self-elevating PowerShell permissions: