Need PowerShell script to export all Dynamic DL in Office 365

Team,

We are trying to build a script where we need to export all the dynamic distribution lists in Office 365 in to one single csv file.
Please help in sharing a script to export all the dynamic distribution lists in Office 365 in to one single csv file.

Vel

Hi Vel, welcome to the forum :wave:

This forum is for scripting questions. We do not write scripts on request and we expect you to have attempted to write some code to solve your problem.

If you have a question, please let us know what you’ve already tried and share your code.

Thanks for the reply,

I have the below script to export the members for a single Dynamic Distribution list, but i require where i can export all the Dynamic Distribution list and their members in Office 365.

$DDL = Get-DynamicDistributionGroup -Identity “Project X”
Get-Recipient -RecipientPreviewFilter ($DDL.RecipientFilter) -OrganizationalUnit ($DDL.RecipientContainer)

Just get all Dynamic Distribution Groups and pipe them to Get-Recipient:

Get-DynamicDistributionGroup | 
    ForEach-Object {Get-Recipient -RecipientPreviewFilter ($_.RecipientFilter) -OrganizationalUnit ($_.RecipientContainer)}
1 Like

Thank you very much, how to get the Dynamic Distribution name also in the output, currently we are getting, Username and mailbox type.

Please advise.

Thanks
Vel

Hi Matt,
The above script gives output of Username and mailbox type, i like to have Dynamic Distribution group names and email address of the members in the Dynamic Distribution group.

Please help what modification we need to do in the script to get the Dynamic Distribution group names and email address of the members in the Dynamic Distribution group.

Thanks
Vel

Your other request for getting AD group members and their e-mail addresses is very similar, the logic is the same. You need to store the group name in a variable, and then output a custom object:

#Requires -Modules ExchangeOnlineManagement

Connect-ExchangeOnline -UserPrincipalName 'admin@mydomain.onmicrosoft.com'

$dynamicDistributionGroups = Get-DynamicDistributionGroup

foreach ($group in $dynamicDistributionGroups) {
    $recipients = Get-Recipient -RecipientPreviewFilter ($group.RecipientFilter) -OrganizationalUnit ($group.RecipientContainer)
    [PSCustomObject] @{
        GroupName  = $group.Name
        Recipients = $recipients.primarySMTPAddress
    }
}

Hi Matt,
Thanks for the script, i am getting error when i use export-csv
An empty pipe element is not allowed, please help me to get all groups and members in a CSV file

For eg:
Group name members
DDG1. Xyz
DDG2. Abc

Thanks
Vel

What does your code look like?

Hi Matt,
I am using the script shared by you, u just included export option to return the output

#Requires -Modules ExchangeOnlineManagement

Connect-ExchangeOnline -UserPrincipalName 'admin@mydomain.onmicrosoft.com'

$dynamicDistributionGroups = Get-DynamicDistributionGroup

foreach ($group in $dynamicDistributionGroups) {
    $recipients = Get-Recipient -RecipientPreviewFilter ($group.RecipientFilter) -OrganizationalUnit ($group.RecipientContainer)
    [PSCustomObject] @{
        GroupName  = $group.Name
        Recipients = $recipients.primarySMTPAddress
    }
} | Export-csv - path "c:\dl.csv" - NoTypeinformation

I am trying to get output as below.

Group1 Member1
Group1. Member2

Thanks
Vel

Ok, the Export-CSV is in the wrong place. However, the code as it stands will output all recipients on one line. For the output you require, try this:

#Requires -Modules ExchangeOnlineManagement

Connect-ExchangeOnline -UserPrincipalName 'admin@mydomain.onmicrosoft.com'

$dynamicDistributionGroups = Get-DynamicDistributionGroup

foreach ($group in $dynamicDistributionGroups) {
    $recipientList = Get-Recipient -RecipientPreviewFilter ($group.RecipientFilter) -OrganizationalUnit ($group.RecipientContainer)
    foreach ($recipient in $recipientList) {
        [PSCustomObject] @{
            GroupName = $group.Name
            Recipient = $recipient.primarySMTPAddress
        } | Export-CSV E:\temp\recipients2.csv -Append -NoTypeInformation
    }
}

Hi Matt,
Thanks for the script.
But I am getting the warning: By default, only the first 1000 items are returned, where should I include -Resultsize Unlimited in the script to get all the data.

Thanks
Vel

You are allowed to read the help by yourself! :smirk:

Please read it COMPLETELY INCLUDING THE EXAMPLES!

2 Likes

Hi Matt,
Need a help, how to export the Powershell output to python data frame.

Thanks
Vel

Please keep in mind we cannot see your screen or your environment and we cannot read your mind.

If you have code you have issues with - please share this code here - formatted as code - along with an explanation what’s not working as expected and along with the error messages you might get -formatted as code as well please.

What python data frame?

A common case to output structured data to be used in other tools is Export-Csv. Please read the help copmpletely including the examples top learn how to use it:

Sorry, Vel. I have no idea what a python data frame is.