Private/TestElevated.ps1

function TestElevated {
    [CmdletBinding()]
    param ()

    $elevated = $false

    try {
        $currentIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()

        if ($currentIdentity.Name -eq "NT AUTHORITY\SYSTEM") {
            Write-Verbose "We are executing as NT AUTHORITY\SYSTEM."
            return $true
        }

        Write-Verbose "Checking to ensure that the current identity ($($currentIdentity.Name)) is in the Administrator role."
        $currentWindowsPrincipal = New-Object "System.Security.Principal.WindowsPrincipal" $currentIdentity
        $elevated = $currentWindowsPrincipal.IsInRole([System.Security.Principal.WindowsBuiltinRole]::Administrator)
    } catch {
        throw
    }

    return $elevated
}

# Copyright (c) 2023 AJ Tek Corporation. All Rights Reserved.