Skip to content

Instantly share code, notes, and snippets.

@techthoughts2
Last active May 7, 2018 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techthoughts2/47ec2022d641057462064ff035319065 to your computer and use it in GitHub Desktop.
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
<#
.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