Will My PowerShell Script for Extracting Distribution Lists Work?

0
9
Asked By CuriousCoder99 On

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

Answered By DebugMaster23 On

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.

Answered By FormattingFanatic On

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!

Answered By CodeCritic99 On

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.

Answered By TechTweaker42 On

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!

Answered By ScriptGuru88 On

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.