private/Test-Administrator.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function Test-Administrator {
    <#
        .SYNOPSIS
        Tests if the current user is running as an administrator on this machine.

        .DESCRIPTION
        Tests if the current user is running as an administrator on this machine.

        .EXAMPLE
        Test-Administrator
    #>


    Write-Verbose -Message "Testing for administrative rights."
    $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = New-Object System.Security.Principal.WindowsPrincipal($identity)
    $admin = [System.Security.Principal.WindowsBuiltInRole]::Administrator
    $IsAdmin = $principal.IsInRole($admin)
    return $IsAdmin
}