Skip to main content

This is an example of using the active-directory powershell module to query directory information.

This script queries the Connect Carolina group structure for School of Pharmacy divisions, designated by a number between 450000 and 459999, and counts the members of each division role (staff, faculty or affiliate).

Duplicates are suppressed, if an account has multiple roles/appointments, only the first occurrence is counted.


#extraneous description info
 $r_prefix = " UNC Eshelman School of Pharmacy-"
 $r_suffix = ", Organizations, UNC Chapel Hill"

 $temp = get-adgroup -filter "name -like 'msg_unc-org-45*-faculty' -or name -like 'msg_unc-org-45*-staff' -or name -like 'msg_unc-org-45*-affiliate'" -Properties "Description"
 $groups = @()
 $totalN = 0

 foreach ($t in $temp){
     if($t.Name -notlike 'msg_unc-org-4501-*'){
         $groups += $t

 $users = @()
 foreach ($g in $groups){
     $n = 0
     foreach ($m in Get-ADGroupMember -Identity $g){
         $duplicate = $false
         foreach ($u in $users){
             if($m.distinguishedName -eq $u.distinguishedName){
                 $duplicate = $true
         if($duplicate -ne $true){
             $users += $m

     $desc = $g.Description.replace($r_prefix," ").replace($r_suffix,"")
     write-output "$desc, $n"
     $totalN += $n
 write-output "Total FTEs: $totalN"

Comments are closed.