none
Looking for a script to retrieve all mail contacts and its mail group memberships

    Question

  • Need to inventory all contacts (external recipients) and find out if each is in any of DLs.  I can get a list of mail contacts (Get-MailContact).  But Exchange 2016 mail contact object has no attribute for group memberships.  Looks like I have to query AD for object type of Contact.  Is that correct?  The query has to be based on display name since mail contact has no AD account (SAM ID).  Can anyone recommend a script?

    Thanks.

    Wednesday, May 15, 2019 10:24 PM

All replies

  •  get-adobject -filter "objectclass -eq 'contact'" -Properties memberof |
         select -expand memberof


    \_(ツ)_/

    • Proposed as answer by jrvModerator Friday, May 17, 2019 3:53 AM
    Wednesday, May 15, 2019 10:50 PM
    Moderator
  • 1) how to export display name, email address along with memberof?  

    2) the output of memberof is listed as LDAP format, such as CN=xx, OU=xxx, DC=xxxx. How can I make it just list the group name?

    3) I found some contacts are in more than one groups.  From the output of memberof I think it only showed the primary group.


    Thursday, May 16, 2019 4:54 AM
  • Hi,

    Thanks for your question.

     Please refer the script below:

    $contacts=Get-ADObject -Filter "objectclass -eq 'contact'" -Properties *
    $ADgroups=$contacts | select -ExpandProperty memberof | Get-ADGroup | select -ExpandProperty name
    $contacts | select -Property displayname,mail,@{n="memberofgroup";e={$ADgroups}}

    Best regards,

    Lee


    Just do it.

    Thursday, May 16, 2019 9:51 AM
    Moderator
  • Hi Lee,

    I tested the commands.  Somehow, the output of "memberofgroup" showed each contact with the same groups, multiple groups.  And after the 3rd group name, the 4th one only showed partial.  Any idea how to fix it?

    Thanks,

    Friday, May 17, 2019 3:22 AM
  • Hi,

    Thanks for your reply.

    Sorry. I have changed my script.

    foreach($contact in $contacts)
    {
    $ADgroups=$contact | select -ExpandProperty memberof | Get-ADGroup | select -ExpandProperty name
      $contact | ft -Property displayname,mail,@{n="memberofgroup";e={$ADgroups}} -Wrap  
    
    }

    Best regards,

    Lee



    Just do it.

    Friday, May 17, 2019 6:22 AM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, May 20, 2019 6:48 AM
    Moderator