PowerShell – Detect if Administrator

email me

Code.ps1

 

# MrNetTek
# eddiejackson.net
# 6/11/2022
# free for public use
# free to claim as your own

# DETECT IF FORM IS LAUNCH AS ADMIN
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())

$currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (!$currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    $message = 
    [System.Windows.MessageBox]::Show("Non-admin detected! `r`n`r`nPlease right-click on app and run as administrator."," ADMIN REQUIRED")
    exit
}