3

Is there a cmdlet or some other way to find out whether my current PowerShell console runs as administrator, i.e. elevated?

bahrep
  • 29,961
  • 12
  • 103
  • 150

1 Answers1

2

Try this code:

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

Attribution: https://www.itprotoday.com/powershell/check-elevation-powershell

bahrep
  • 29,961
  • 12
  • 103
  • 150
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • Yes, this code works. Thank you! – bahrep Feb 11 '20 at 15:52
  • Please accept the answer clicking big tick mark and upvote it. Thank you. – Wasif Feb 11 '20 at 16:39
  • 1
    Is it necessary to include "New-Object"? I have always just done it without that, like so. This will either return $true or $false : ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") – AutomatedOrder Feb 11 '20 at 17:11