Checking if User has Admin Rights When Running a PowerShell Script

For my Active Directory documentation script, if the user requests hardware inventory for the domain controllers, the user must run the script with domain administrator credentials. How do you determine if a script is being run with Domain Admin rights?

If(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrators"))
{
#user is a member of local administrators
}
Else
{
#user is not a member of local administrators
}

For testing if the user is in the Domain Admins group:

If(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Domain Admins"))
{
#user is a member of domain admins
}
Else
{
#user is not a member of domain admins
}

Hope this helps.

Webster

About Carl Webster

Webster is an independent consultant in the Nashville, TN area and specializes in Citrix, Active Directory and Technical Documentation. Webster has been working with Citrix products for many years starting with Multi-User OS/2 in 1990.