Created
April 18, 2016 09:58
-
-
Save AndiSHFR/b5cb79bb455723cbfe19dec336d6c15c to your computer and use it in GitHub Desktop.
Test for admin privileges in powershell.
This file contains hidden or 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
function Test-IsAdmin { | |
try { | |
$identity = [Security.Principal.WindowsIdentity]::GetCurrent() | |
$principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity | |
return $principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) | |
} catch { | |
throw "Failed to determine if the current user has elevated privileges. The error was: '{0}'." -f $_ | |
} | |
<# | |
.SYNOPSIS | |
Checks if the current Powershell instance is running with elevated privileges or not. | |
.EXAMPLE | |
PS C:\> Test-IsAdmin | |
.OUTPUTS | |
System.Boolean | |
True if the current Powershell is elevated, false if not. | |
#> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment