Quantcast
Channel: Software Development – The Blog of Colin Mackay
Viewing all articles
Browse latest Browse all 34

How to: Tell if a PowerShell script is running as the Administrator

$
0
0
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)))
{
    Write-Warning "This script needs to be running as the administrator."
    Exit 1
}

Write-Host "You are running as the administrator."

This script gets the current Windows Identity, then queries it to find out if it has the appropriate role.


Viewing all articles
Browse latest Browse all 34

Trending Articles