public/Test-Administrator.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<#
    .Synopsis
        Test to see if the current user is an administrator.
    .Description
        Test to see if the current user is an administrator.
    .Example
        PS C:\> Test-Administrator
        Test to see if the current user is an administrator and returns a boolean.
#>

function Test-Administrator {
    [CmdletBinding()]
    [OutputType([Boolean])]
    param (
    )

    begin {
        Write-LogMessage -Message "Started execution"
    }

    process {
        $currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
        return (New-Object Security.Principal.WindowsPrincipal $currentUser).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    }

    end {
        Write-LogMessage -Message "Finished execution"
    }
}