Skip to content

Instantly share code, notes, and snippets.

@YakDriver
Created March 23, 2018 11:15
Show Gist options
  • Save YakDriver/2c584a44b5b7be9ba8ae526d2243909a to your computer and use it in GitHub Desktop.
Save YakDriver/2c584a44b5b7be9ba8ae526d2243909a to your computer and use it in GitHub Desktop.
Is PowerShell script running as administrator? Find out from inside the script.
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
Write-Host "Running as Administrator"
}
else
{
Write-Host "NOT Running as Administrator"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment