Script To Get Common Recipients Between Distribution Groups

Gautham Allur Subramanya  is PFE from India, and shares a PowerShell script used to report on common recipients between two different distribution groups in Exchange.


When delivering a recent PowerShell WorkshopPLUS engagement at a customer site, one of the Exchange administrators wanted to know how to write a script which would generate a text file of common recipients between two different distribution groups in Exchange.  Here is a something I came up with that compares two different distribution group’s members and generates a text file with the common recipients of the two. 

This is meant to help you get a good idea to get the desired output, please use your discretion while using this in a production environment.  This requires PowerShell version 4.0.  Only install supported versions on Windows Management Framework/PowerShell onto Exchange.  Supported versions are defined in the Exchange Server Supportability Matrix

 

#Creating a Function

function Get-CommonUsers

{

Write-Host "Enter Distribution Group 1"

$DistGrp1 = Read-Host #First Distribution Group DG1

Write-Host "Enter Distribution Group 2"

$DistGrp2 = Read-Host #Second Distribution Group DG2

$DistGrp1Members = Get-DistributionGroupMember $DistGrp1

$DistGrp2Members = Get-DistributionGroupMember $DistGrp2

$DG1NamesOnly = $DistGrp1Members.name #Getting only the names of recipients

$DG2NamesOnly = $DistGrp2Members.name

#PSItem is a built-in pipeline variable

$DG1NamesOnly | ForEach-Object {if($PSItem -in $DG2NamesOnly) #Searching DG1 items in DG2

{

$PSItem | Out-File -FilePath "C:\Common Users.txt" -Append #Appending matches to text file

}

}

$CommonRecipientCount = (Get-Content "c:\Common Users.txt" | Measure-Object).count #Number of matches

Add-Content -Path "c:\Common Users.txt" -Value $CommonRecipientCount

#Append number of matches to text file

}

 

This will define a function to get the desired output. We need to call the function to get the desired output.

We can call the function by using the function name we have defined – Get-CommonUsers

You will be prompted to enter the distribution group names, no need to change any of the variables.

We will get an output with all the recipients that are common to two distribution groups as well the count of such recipients in a text file in the ‘C’ drive with the name ‘Common Users.txt’. If you do not want the count, then the last two lines of function could be left out.

If you are familiar with PowerShell and want to change the name of the text file generated or the place where it is stored, you can do so by editing the function appropriately.

I’ve made a function to get the desired output, so that it could be easily reused. If you or your customer wants a similar output, you can look at this as good starting point.

Further, it can be saved as a script, but ensure that you have the right execution policy settings so that you can run it.

 

DISCLAIMER:

This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.  THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.  We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.
Please note: None of the conditions outlined in the disclaimer above will supersede the terms and conditions contained within the Premier Customer Services Description.
This posting is provided “AS IS” with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.

 


Published by MSPFE editor Rhoderick Milne


Comments