Created
March 26, 2021 11:39
-
-
Save Hashbrown777/0eb71eb07d9b56a68db93d1bda2ae899 to your computer and use it in GitHub Desktop.
when put at the top of a script ensures it is [re]ran as admin
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
#at top of script | |
if (! | |
#current role | |
(New-Object Security.Principal.WindowsPrincipal( | |
[Security.Principal.WindowsIdentity]::GetCurrent() | |
#is admin? | |
)).IsInRole( | |
[Security.Principal.WindowsBuiltInRole]::Administrator | |
) | |
) { | |
#elevate script and exit current non-elevated runtime | |
Start-Process ` | |
-FilePath 'powershell' ` | |
-ArgumentList ( | |
#flatten to single array | |
'-File', $MyInvocation.MyCommand.Source, $args ` | |
| %{ $_ } | |
) ` | |
-Verb RunAs | |
exit | |
} | |
#example program, this will be ran as admin | |
$args | |
Pause | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment