- 1 year ago
How can I find out whether my current PowerShell console runs elevated (as administrator)
To find out whether your current PowerShell console is running elevated (as administrator), you can use the $env:USERNAME
and [Security.Principal.WindowsPrincipal]
classes in PowerShell. Here's an example:
powershell$currentUser = $env:USERNAME
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "The current PowerShell console is running as administrator for user: $currentUser"
} else {
Write-Host "The current PowerShell console is not running as administrator for user: $currentUser"
}
The above code checks if the current user belongs to the Administrator
role using the [Security.Principal.WindowsBuiltInRole]::Administrator
value. If the user is in the Administrator
role, it means the PowerShell console is running elevated (as administrator).
Note that this check is based on the user's role and not the actual process elevation level. It assumes that the user running the PowerShell console has administrator privileges. Keep in mind that the PowerShell process itself might not be elevated, but the user account running it is in the Administrator
role.
By executing this code in your PowerShell console, you will get an output indicating whether the current PowerShell console is running elevated or not.