Skip to content

Instantly share code, notes, and snippets.

@Hashbrown777
Created March 26, 2021 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hashbrown777/0eb71eb07d9b56a68db93d1bda2ae899 to your computer and use it in GitHub Desktop.
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
#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