FindMissingGroupMembers.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#requires -version 5
<#
.SYNOPSIS
  Script to find users missing from distribution groups
.DESCRIPTION
  -Script builds a list of mailboxes and distribution groups. Change the line Where-Object {$_.Name -eq 'Epic Group All Users' -or $_.Name -eq 'Epic Residential All Users' -or $_.Name -eq 'EREC All Users20210429162618' -or $_.Name -eq 'Reddot All Users20190605001136' -or $_.Name -eq 'SumoQuote All Users20200321042803'}
  to suit your purposes
  -Loops through each distribution group and finds members
  -Compares the mailboxes in group members. Many missing from the distribution groups you specify will be on the left side
.PARAMETER <Parameter_Name>
     
.INPUTS
  None
.OUTPUTS
  None
.NOTES
  Version: 1.0
  Author: Chris Betts
  Creation Date: 6/11/2021
  Purpose/Change: Initial script development
   
.EXAMPLE
#>


    $groupMembers = @()
    $userList = @()

    #Get a list of all email addresses in the system
    $userList = Get-Mailbox | Select-Object -Property PrimarySMTPAddress

    #build a list of distribution groups
    $distroGroup = Get-DistributionGroup | 
    Where-Object {$_.Name -eq 'Epic Group All Users' -or $_.Name -eq 'Epic Residential All Users' -or $_.Name -eq 'EREC All Users20210429162618' -or $_.Name -eq 'Reddot All Users20190605001136' -or $_.Name -eq 'SumoQuote All Users20200321042803'}   


    #get all group members for each distribution group
    foreach ($group in $distrogroup) {
        $groupMembers += Get-DistributionGroupMember -Identity $group.Name | 
        Select-Object @{Name='Distribution Group'; Expression={$group.Name}}, Name, PrimarySMTPAddress
    }

    #compare the email addresses in the lists. Those not in $distrogroup groups will be on the left hand side
    Compare-Object -ReferenceObject $userList -DifferenceObject $groupMembers -Property @{Expression = {$_.PrimarySMTPAddress}} -IncludeEqual