Check for elevation in PowerShell

Check for elevation in PowerShell

Q. How can I check if my PowerShell session is elevated using Security.Principal.WindowsIdentity?

A. To easily check for elevation use the following PowerShell snippet of code:

if ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
  Write-Output "Elevated."
}
else
{
  Write-Output "Not elevated."
}

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish