I created a PowerShell script aimed at extracting all distribution lists along with their members and owners. I'm looking for feedback on whether it should work as is, or if I've missed anything important. Here's the script:
```powershell
# Connect to Exchange Online
Connect-ExchangeOnline
$Report = @()
$Groups = Get-DistributionGroup -ResultSize Unlimited
foreach ($Group in $Groups) {
if ($Group.RecipientTypeDetails -eq "DynamicDistributionGroup") { continue }
$OwnerNames = @()
foreach ($Owner in $Group.ManagedBy) {
$OwnerRecipient = Get-Recipient $Owner
$OwnerNames += $OwnerRecipient.DisplayName
}
$OwnersString = $OwnerNames -join "; "
$Members = Get-DistributionGroupMember -Identity $Group.Identity -ResultSize Unlimited
foreach ($Member in $Members) {
$Report += [PSCustomObject]@{
DistributionList = $Group.DisplayName
GroupEmail = $Group.PrimarySmtpAddress
Owners = $OwnersString
MemberName = $Member.DisplayName
MemberEmail = $Member.PrimarySmtpAddress
MemberType = $Member.RecipientType
}
}
}
$Report | Export-Csv "C:UsersDocumentsDL_Members_Owners_Report.csv" -NoTypeInformation
```
I'm open to any suggestions or improvements!
5 Answers
I wouldn't just take it for granted that it works. It's better to execute the script line by line; that way, you'll understand what each part is doing. That kind of debugging is invaluable for learning and improving your skills.
Just a heads-up on your formatting: the three-backtick code fence you used might not display well on some platforms. Try pasting the code using a PowerShell editor, highlighting it, and hitting tab to indent it, or use four spaces at the start of each line for proper formatting. Here's a handy [Formatting Guide](https://support.reddithelp.com/hc/en-us/articles/360043033952-Formatting-Guide) to help you out!
Why not just run the code yourself and see if there's an issue? Going through it yourself will help you catch any errors directly instead of waiting for feedback from others.
Honestly, I think it could work, but you should definitely run it to see the output for yourself. There are some common pitfalls you might want to look out for:
- Declaring empty arrays can lead to performance issues later.
- Instead of using `$yyy += zzz`, try building your objects in a more efficient way to avoid slowdowns.
- If you're doing a lot of things in loops, pay attention to how you name your variables to avoid confusion and ensure clarity.
Nothing major, just keep these points in mind!
This looks like it should work for generating a CSV file with the details you want. One suggestion is to consider using `Get-EXORecipient` instead, as it might offer better performance.
I also find it helpful to list each owner and member on separate rows, including columns for DL name, DL SMTP, user name, user email, and permissions. This setup makes it easier for anyone to read and filter in Excel, especially if you're trying to find specific users in multiple groups.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically