I'm trying to get a clearer view of the members in my distribution lists. I initially attempted exporting the data to a CSV file, but it only shows everything in one line as {Name1, Name2, Name3}. Here's the code I'm using:
$DistMembers1 = Get-DistributionGroupMember -Identity "[email protected]"
$DistMembers2 = Get-DistributionGroupMember -Identity "[email protected]"
$DistMembers3 = Get-DistributionGroupMember -Identity "[email protected]"
$DistListMembers = [PSCustomObject]@{
Dist1 = $DistMembers1.Name
Dist2 = $DistMembers2.Name
Dist3 = $DistMembers3.Name
}
$DistListMembers | FT
I'm looking for a better way to have these members displayed, as each distribution list member should ideally be in its own row. I've tried looking up solutions but I'm struggling to find the right terms or methods. Any suggestions would be greatly appreciated!
3 Answers
Just a note about the formatting: if the output seems truncated, it might be due to the width of the console window. Each object will be treated as a single row in a table format, so ensure you have enough space to see everything! Consider exporting to CSV or using `Out-GridView` if you need a more interactive view.
Instead of using individual arrays for each distribution list, you can loop through them more effectively. Here’s a suggested approach:
```PowerShell
$distlists = @(
"[email protected]",
"[email protected]",
"[email protected]"
)
$report = foreach ($list in $distlists) {
$members = Get-DistributionGroupMember -Identity $list
foreach ($mbr in $members) {
[PSCustomObject]@{
DistributionList = $list
Member = $mbr.Name
}
}
}
$report | Export-Csv -Path 'pathtowhereveryouwanttosavethefile.csv'
```
This will create a new object for each member, making the export much cleaner!
You're right that using `Format-Table` messes things up for exporting. Instead, you can create a loop that processes each member separately. Try this:
```PowerShell
$DistListIdentities = @(
"[email protected]",
"[email protected]",
"[email protected]"
)
$DistListMembers = foreach ($Identity in $DistListIdentities) {
Get-DistributionGroupMember -Identity $Identity | ForEach-Object {
[PSCustomObject]@{
DistributionList = $Identity
MemberName = $_.Name
MemberId = $_.UserId
}
}
}
$DistListMembers | Export-Csv C:PathToYourFile.csv -NoTypeInformation
```
This should give you a proper CSV with each member in their own row!
Thanks for the tip! I'll make sure to check the console size. Looking forward to seeing it all laid out nicely.