Skip to content

Instantly share code, notes, and snippets.

@zblesk
Last active April 15, 2019 09:24
Show Gist options
  • Save zblesk/c169a3cdac4d0e9c118f to your computer and use it in GitHub Desktop.
Save zblesk/c169a3cdac4d0e9c118f to your computer and use it in GitHub Desktop.
A self-elevating powershell script. Makes sure current script is running as admin.
if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Set-Location $PSScriptRoot
"Running as admin in $PSScriptRoot"
}
else
{
"NOT running as an admin!"
Start-Process powershell -WorkingDirectory $PSScriptRoot -Verb runAs -ArgumentList "-noprofile -noexit -file $PSCommandPath"
return "Script re-started with admin privileges in another shell. This one will now exit."
}
"Doing this only as admin."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment