Sometimes you have to verify if your PowerShell session runs with elevated permissions. With this short code you can check it and inform the user or stop the script if elevated permissions are required.

If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    [Security.Principal.WindowsBuiltInRole] "Administrator"))
{    
    Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
    Break
}

Leave a Reply

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