PowerShell: Test for Elevation

How can we test if the user is running our script elevated? The following function is a small, quick way to get this information.

using namespace System.Security.Principal

function Test-IsElevated() {
    [CmdletBinding()]
    param ()
    process {
        # Get the Windows Identity
        $ID = [System.Security.Principal.WindowsIdentity]::GetCurrent()

        # Get the Windows Principal
        $WP = [System.Security.Principal.WindowsPrincipal]($ID)

        # true if the user is in the Administrator role, otherwise false
        $WP.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }
}

Categories: PowerShell
Wayne Boyles

Written by:Wayne Boyles All posts by the author

Leave a reply

Your email address will not be published. Required fields are marked *