Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active March 13, 2018 00:20
Show Gist options
  • Save Jaykul/f2abefe8530cbca72296697bdc2b329e to your computer and use it in GitHub Desktop.
Save Jaykul/f2abefe8530cbca72296697bdc2b329e to your computer and use it in GitHub Desktop.
What's the simplest way to test if we're currently running "As Administrator"?
#.Synopsis
# Returns TRUE if the current PowerShell is running elevated, FALSE otherwise.
[CmdletBinding()]param()
$IsElevated = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if($IsElevated -ne ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Administrators")) {
Write-Warning "String Method does not work"
}
if($IsElevated -ne [Bool]([Security.Principal.WindowsIdentity]::GetCurrent().Groups -eq "S-1-5-32-544")) {
Write-Warning "Group SID does not work"
}
if($IsElevated -ne ([Security.Principal.WindowsIdentity]::GetCurrent().Owner -eq 'S-1-5-32-544')) {
Write-Warning "Current.Owner does not work"
}
return $IsElevated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment