Last active
May 7, 2018 18:23
-
-
Save techthoughts2/47ec2022d641057462064ff035319065 to your computer and use it in GitHub Desktop.
Evaluates if current PowerShell session is running under the context of an Administrator
This file contains 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
<# | |
.Synopsis | |
Tests if PowerShell Session is running as Admin | |
.DESCRIPTION | |
Evaluates if current PowerShell session is running under the context of an Administrator | |
.EXAMPLE | |
Test-RunningAsAdmin | |
This will verify if the current PowerShell session is running under the context of an Administrator | |
.EXAMPLE | |
Test-RunningAsAdmin -Verbose | |
This will verify if the current PowerShell session is running under the context of an Administrator with verbose output | |
.OUTPUTS | |
System.Boolean | |
.NOTES | |
Author: Jake Morrison - @jakemorrison - http://techthoughts.info | |
#> | |
function Test-RunningAsAdmin { | |
[CmdletBinding()] | |
Param() | |
$result = $false #assume the worst | |
try { | |
Write-Verbose -Message "Testing if current PS session is running as admin..." | |
$eval = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
if ($eval -eq $true) { | |
Write-Verbose -Message "PS Session is running as Administrator." | |
$result = $true | |
} | |
else { | |
Write-Verbose -Message "PS Session is NOT running as Administrator" | |
} | |
}#try | |
catch { | |
Write-Warning -Message "Error encountering evaluating runas status of PS session" | |
Write-Error $_ | |
}#catch | |
return $result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment