Get Distribution Group membership for specific users from a csv file

mark terry 185 Reputation points
2023-10-16T23:19:35.4766667+00:00

I have an input file with the email addresses of 1000's of Exchange Mailboxes and Exchange External Contacts e.g.

EmailAddress

******@internal-mailbox.com

******@external-contact.com

....

....

....

I would like to be able to query ALL Distribution Groups in my Organization and output the email addresses of the Distribution Groups which contain any of the users and/or contacts in the input file AND show which DGs each of the users belong to (example output file below):

Distribution Group Address;Member Address

Sales;******@internal-mailbox.com

Marketing;******@external-contact.com

Everyone;******@internal-mailbox.com

Everyone;******@external-contact.com

Thanks

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,180 questions
Exchange | Exchange Server | Management
0 comments No comments
{count} votes

Accepted answer
  1. Vasil Michev 119.6K Reputation points MVP Volunteer Moderator
    2023-10-17T06:29:14.1733333+00:00

    Depending on the number of groups and users/contacts you want to run this against, there are two approaches to try:

    1. Enumerate the membership of all groups in question, store it in hashtable(s) and then compare it against each user/contact.
    2. Use the Get-Recipient cmdlet to get the "memberof" list for each user/contact:

    Get-Recipient -Filter "Members -eq '$dn'"

    where you need to provide the DistinguishedName value for the object.

    Hard to tell which method will be more efficient without knowing details about your environment. Here's a ready to use script that leverages method #2, feel free to edit it to better suit your needs: https://www.michev.info/blog/post/4222/generating-a-report-of-users-group-membership-memberof-inventory-v2


1 additional answer

Sort by: Most helpful
  1. Kael Yao 37,746 Reputation points Moderator
    2023-10-17T06:23:55.3733333+00:00

    Hi @mark terry,

    Please have a check if the following script works for you:

    $users = Import-csv "C:\temp\user.csv"
    
    foreach ($user in $users) {
        $Groups = Get-DistributionGroup -ResultSize Unlimited
        $result = @()
    
        foreach ($Group in $Groups) {
            $GroupMembers = Get-DistributionGroupMember $Group.Name | foreach {$_.PrimarySmtpAddress}
            if ($GroupMembers -contains $user.EmailAddress) {
                $result += New-Object PSObject -Property @{
                    "Distribution Group Address" = $Group.PrimarySMTPAddress
                    "Member Address" = $user.EmailAddress
                }
            }
        }
    
        $result | Export-csv -Append C:\temp\membership.csv
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.