Get-DistributionGroup

Roger Roger 6,571 Reputation points
2024-11-01T12:57:36.7733333+00:00

Hi All,

I have a list of Distribution Lists (DLs) in a CSV file in the following format:

DLList
dl1@contoso.com
dl2@contoso.com

I want to import this CSV file and retrieve the following information for each DL:

  1. The count of DL members, including nested DLs.
  2. The delivery management settings, specifically the sender options for the DL, such as:
    • "Only allow messages from people inside my organization" or
      • "Allow messages from people inside and outside my organization."
      1. If any DL has specified senders, I need a list of those users.

I also need to export this information to a CSV file. I am currently using the script below, which provides the DL member count (including nested DLs) for each DL. please guide me how to fetch this information.

# Define the distribution group
$groupName = "mydl@contoso.com"
# Get the distribution group object
$group = Get-DistributionGroup -Identity $groupName
 
# Get all members of the distribution group
$members = Get-DistributionGroupMember -Identity $group -ResultSize Unlimited
 
# Initialize a counter for total members
$totalMembers = 0
 
# Loop through each member
foreach ($member in $members) {
    # Increment the counter
    $totalMembers++
     # Check if the member is a group
    if ($member.ObjectClass -eq "Group") {
        # Recursively get members of the nested group
        $nestedMembers = Get-DistributionGroupMember -Identity $member.DistinguishedName -ResultSize Unlimited
        $totalMembers += $nestedMembers.Count
    }
}
 # Output the total member count
Write-Output "Total members including nested groups: $totalMembers" 

Microsoft Exchange Online
Exchange Server
Exchange Server
A family of Microsoft client/server messaging and collaboration software.
1,382 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,651 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,721 questions
Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
2,175 questions
{count} votes

Accepted answer
  1. Bruce Jing-MSFT 7,175 Reputation points Microsoft Vendor
    2024-11-04T07:00:26.7766667+00:00

    Hi,@Roger Roger

    Thanks for posting your question in the Microsoft Q&A forum.

    1. First, ensure you have the necessary permissions to perform these actions and that you're connected to your Exchange server or Office 365 via PowerShell.
    2. Import the CSV file containing the Distribution Lists using the Import-Csv cmdlet:
    $DLs = Import-Csv -Path "C:\Path\To\Your\File.csv"
    
    
    1. Iterate over each Distribution List in the CSV and retrieve the necessary information. You can use the Get-DistributionGroup and Get-DistributionGroupMember cmdlets to get the details:
    $DLInfo = foreach ($DL in $DLs) {
        $DLName = $DL.DLList
        $Members = Get-DistributionGroupMember -Identity $DLName -ResultSize Unlimited
        $DLSettings = Get-DistributionGroup -Identity $DLName
        
        $MemberCount = $Members.Count
        $DeliveryManagement = $DLSettings.ManagedBy
        $SenderRestrictions = $DLSettings.AcceptMessagesOnlyFrom
        
        [PSCustomObject]@{
            DLName = $DLName
            MemberCount = $MemberCount
            DeliveryManagement = if ($DLSettings.RequireSenderAuthenticationEnabled) {
                "Only allow messages from people inside my organization"
            } else {
                "Allow messages from people inside and outside my organization"
            }
            SpecifiedSenders = $SenderRestrictions -join ", "
        }
    }
    
    
    1. Finally, export the gathered information to a new CSV file using the Export-Csv cmdlet:
    $DLInfo | Export-Csv -Path "C:\Path\To\Your\ExportedFile.csv" -NoTypeInformation
    
    

    Here are my tests:

    User's image

    The result of exporting the file:

    User's image


    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.


0 additional answers

Sort by: Most helpful

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.