To check if the current PowerShell session is running with administrative privileges, you can use the following command:
# Check if the PowerShell session is running as administrator if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Host "PowerShell is running as administrator." -ForegroundColor Green } else { Write-Host "PowerShell is NOT running as administrator." -ForegroundColor Red }
[Security.Principal.WindowsIdentity]::GetCurrent()
retrieves the current Windows identity.[Security.Principal.WindowsPrincipal]
is used to check the roles of the current user.[Security.Principal.WindowsBuiltInRole]::Administrator
checks if the current user is in the Administrator role.To verify manually:
whoami /groups | findstr "S-1-5-32-544"