← script library

Check for Administrator Rights in a PowerShell Script

Checks & DiagnosticsAugust 17, 2026

Resolves the current Windows identity and fails the run when it is not in the Administrators role, with a message that also covers the remote case: a WinRM session started with the wrong credential looks exactly like a local privilege problem. Use it as the guard step in front of anything that changes the system.

powershell
# Ensure Administrator previlegues
#------------------------------------------------------

$isadm = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isadm) {
    Write-Host "<WRITE-LOG = ""*Please run this script as Administrator.*"">"
    Write-Host "<WRITE-LOG = ""*If this was a remote execution please provide Administrator credentials.*"">"
    Write-Error "Warning: Not running as Administrator."
}

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.