Monday, 27 June 2022

Powershell check if script is running as administrator

Write-Host "Checking for elevated permissions..."

if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {

    Write-Warning "Insufficient permissions to run this script."

    Break

}

else {

 <CODE GOES HERE>

}

No comments:

Post a Comment