PowerShell: Admin Privileges Enabled?

If you want to know whether your script has currently full Administrator privileges, here is an (admittedly long) one-liner that tells you:

(New-Object System.Security.Principal.WindowsPrincipal([System.Security.Principal.
WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.
WindowsBuiltInRole]::Administrator)

Try executing this line in a regular PowerShell and then in an elevated shell, and check out the difference. Or, create your own console prompt which turns red when you have admin privileges:

function prompt {
if ((New-Object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
  Write-Host '(Admin)' –ForegroundColor Red -NoNewLin
  $user = 'Administrator: '
} else {
  $user = ''
}
'PS> '
$Host.UI.RawUI.WindowTitle = "$user $(Get-Location)"
}

Leave a Reply

Your email address will not be published.