none
Exchange Script needed to get all DL's that contain external contacts RRS feed

  • Question

  •  Hi,

      I need a script that would report back ALL Distribution Lists that contain at least 1 external member.

    Any ideas?

    Thanks

    Tuesday, September 24, 2019 12:45 PM

Answers

  • This script could help you export the result:

    $Groups = Get-distributiongroup -ResultSize unlimited
    $Temp=@()
    
    foreach($Group in $Groups){  
        $Members = Get-distributionGroupMember $Group
        foreach($Member in $Members){
            if($Member.RecipientType -eq "MailContact")
            {
                $temp2 = $Group.Name+" has external Members"
                $Temp+=$temp2
                break
            }
        }
    }
    $Temp | Out-File c:/temp/resoult.txt

    Regards,

    Kyle Xu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Thursday, September 26, 2019 2:03 AM
    Moderator

All replies

  • Well how you define an external member?
    Tuesday, September 24, 2019 5:24 PM
  • Hi,

    Do you mean Mail Users? If so, this script below will be useful to you:

    $Groups = Get-distributiongroup -ResultSize unlimited
    
    foreach($Group in $Groups){  
        $Members = Get-distributionGroupMember $Group
        foreach($Member in $Members){
            if($Member.RecipientType -eq "MailContact")
            {
                Write-Host $Group "has external Members"
                break
            }
        }
    }

    Regards,

    Kyle Xu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Wednesday, September 25, 2019 8:17 AM
    Moderator
  • by external member I mean a mail contact. 
    Wednesday, September 25, 2019 3:32 PM
  • not Mail User, but Mail Contact. 
    Wednesday, September 25, 2019 3:33 PM
  • how to I write the output to a file?

    Wednesday, September 25, 2019 3:42 PM
  • So in a nutshell, you need to get the list of all group members, then check if any of those is a MailContact and return only such groups. The following one-liner shows you how this can be done:

    Get-DistributionGroup | % { if (Get-DistributionGroupMember $_.PrimarySmtpAddress | ? {$_.RecipientTypeDetails -eq "MailContact"}) {$_.PrimarySmtpAddress}} 

    However, I would strongly suggest you spent some time to develop a full-blown script instead, as the example above will not work well if you have large number of groups, or groups with tons of members. Not to even mention nested groups.

    Wednesday, September 25, 2019 6:09 PM
  • Thanks.

    Kyles script above seems to work, but it writes to the screen. How can i output that to a file?

    Wednesday, September 25, 2019 6:53 PM
  • This script could help you export the result:

    $Groups = Get-distributiongroup -ResultSize unlimited
    $Temp=@()
    
    foreach($Group in $Groups){  
        $Members = Get-distributionGroupMember $Group
        foreach($Member in $Members){
            if($Member.RecipientType -eq "MailContact")
            {
                $temp2 = $Group.Name+" has external Members"
                $Temp+=$temp2
                break
            }
        }
    }
    $Temp | Out-File c:/temp/resoult.txt

    Regards,

    Kyle Xu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Thursday, September 26, 2019 2:03 AM
    Moderator
  • Hi,

    I am writing here to confirm with you how thing going now?

    If the above suggestion helps, please be free to mark it as an answer for helping more people.

    Regards,

    Kyle Xu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Wednesday, October 9, 2019 2:34 AM
    Moderator
  • the script work, thanks. However it outputs in the txt file all next to each other on the same line, then goes to the next line and continues. How can I output each on it's own row?

    Monday, November 11, 2019 8:52 PM
  • Also, is there a way to configure that script to show only DL's that don't have any MailContacts? Just internal users.
    Tuesday, November 12, 2019 7:04 PM