Verify if PowerShell run as an Admin

Hello,

You can verify if PowerShell run as an admin with those few lines :

$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal]$identity 
if($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)){"Admin"}  else {"Not an admin"}

That can be useful if you need backward compatibility with PowerShell 2.

If you don’t need the PowerShell 2 compatibility, you can use :

#Requires –RunAsAdministrator

 

Leave a Reply