There are a lot of errors in it, you could compare with this one:
$DL1S = Get-DistributionGroupMember "MYDL@contoso.com" -ResultSize unlimited
$DL2S = Get-DistributionGroup -ResultSize unlimited
$Result = @()
foreach ($DL1 in $DL1S) {
foreach($DL2 in $DL2S){
if ($Dl2.ManagedBy -contains $DL1.Name){
$Result += Get-DistributionGroup $DL2.name | select DisplayName,PrimarySMTPAddress,Notes,managedby
}
}
}
$Result | Export-Csv c:\temp\DistributionLists.csv -NoTypeInformation
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.