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

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.