Count AD group members

Every year we have to count and report licenses on different systems. Since we use centralized Active Directory security groups to decide which users who gain access to RDS servers etc etc… it`s a pretty easy task to count the users in those groups to find out how many CALs we need. When the boss interrupts you more then once it´s time to use powershell to solve it. This script include one level nested groups.

#-------------------------------------------------------------
#NAME: CountAdGroupMembers.ps1
#AUTHOR: Viktor Lindström
#
#COMMENTS: This script counts and summary members i multiple AD groups.
#It also includes one level nested groups
#-------------------------------------------------------------
function count-members
{
param(
[parameter(mandatory)]
[array]$grupper)

$sum = 0

foreach ($grupp1 in $grupper)
{$grupper3 = Get-ADGroupMember $grupp1 | Where objectclass -eq "group"
$grupper += $grupper3
}

foreach ($grupp in $grupper)
{$count = Get-ADGroupMember $grupp | Where objectclass -eq "User"
$sum += $count.count
}
$sum
}