Forum Discussion

Mark Louie Diaz's avatar
Mark Louie Diaz
Copper Contributor
Aug 29, 2018

Get mailcontact if member of any group

Hi Tech Community,

 

May I ask how can I accomplish the subject? I want to know before removing a mail contact if it is a member of a Distribution group.

 

Best Regards,

Mark

  • Here's a fast way to do it:

     

    $dn = (Get-MailContact your_mail_contact).DistinguishedName

    Get-Recipient -Filter "Members -eq '$dn'"

     

    This will return all DGs, mail-enabled SGs and O365 Groups the contact might be a member of. And since it's a server-side filter, you don't need to iterate over each group.

    • Joshua Bines's avatar
      Joshua Bines
      Iron Contributor

      VasilMichevthe need for speed? try this...

       

      Get-ADObject -LDAPFilter "(&(objectCategory=contact)(objectClass=contact)(memberOf=*))" -Properties Name,MemberOf,CanonicalName | ft Name,CanonicalName,memberof -wrap

      • ZK4568746's avatar
        ZK4568746
        Copper Contributor
        Utilizing the LDAP FIlter was smart, it allowed me to act against just the ADObjects that were Contacts with existing group memberships without rewriting the entire script. Thanks!
    • --ZiM-- .'s avatar
      --ZiM-- .
      Copper Contributor

      The following will get you a cleaner list with all mailboxes followed by contacts $m = New-Object System.Collections.ArrayList $c = New-Object System.Collections.ArrayList Get-DistributionGroupMember GroupName | ForEach-Object { if($_.RecipientType.ToString().Contains("Mailbox") -eq $true) { $m.Add($_.Name) } else { if ($_.RecipientType.ToString().Contains( "Contact") -eq $true) { $c.Add($_.Name) } } } $m | Get-Mailbox | ft Name,For* $c | Get-MailContact | ft Name,For*

      https://getappvalley.com/

      https://vidmate.onl/

      https://tweakbox.mobi/

    • Robert Bollinger's avatar
      Robert Bollinger
      Iron Contributor

      VasilMichev 

       

      Hey Vasil, 

       

      On this here: 

       

      $dn = (Get-MailContact your_mail_contact).DistinguishedName

      Get-Recipient -Filter "Members -eq '$dn'"

       

      How would i add an input file to your script? for instance i would like to run the above cmdlets on a list of 122 people. 

      Thanks, 

       

      Robert

       

  • Adam Ochs's avatar
    Adam Ochs
    Steel Contributor

    Hey Mark Louie Diaz,

     

    I think this would work:

     

    $Username = "user@domain.com"

     

    $DistributionGroups= Get-DistributionGroup | where { (Get-DistributionGroupMember $_.Name | foreach {$_.PrimarySmtpAddress}) -contains "$Username"}

    Just toss in the email address for the mail user in there, and it should return any DL that has that user.

     

    Adam

    • wskonieczny's avatar
      wskonieczny
      Copper Contributor

      Dominique0473 

       

      This works for me:

      Get-ADObject -LDAPFilter '(&(objectclass=contact)(!(memberof=*)))' -Server dc01 -SearchBase 'OU=Contacts,OU=Company,DC=work,DC=com' -Properties name

  • Joshua Bines's avatar
    Joshua Bines
    Iron Contributor

    It's funny coming across your own posts again.... I've had questions about how can this be completed for cloud only objects in Exchange Online? This should help and is much better than everything else I've seen ;) Stay safe out there! J   

    $allcontacts = Get-MgContact -all -Property MemberOf -ExpandProperty MemberOf
    $allcontactsingroup =  $allcontacts | ?{$_.memberof -ne $null}
    
    #Print to Screen
    $allcontactsingroup | FT DisplayName,MemberOf
     
    #Export to CSV with some Funkyness (is that even a word?)
    $allcontactsingroup | Select *, @{Name = 'MemberOfToString'; Expression = {$($_.MemberOf.id -join ',')}} | export-csv contactsingroup.csv

     

Resources