Is there a cmdlet or some other way to find out whether my current PowerShell console runs as administrator, i.e. elevated?
Asked
Active
Viewed 232 times
1 Answers
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
-
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
-
1Is 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