Get mailcontact if member of any group

%3CLINGO-SUB%20id%3D%22lingo-sub-239398%22%20slang%3D%22en-US%22%3EGet%20mailcontact%20if%20member%20of%20any%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-239398%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Tech%20Community%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMay%20I%20ask%20how%20can%20I%20accomplish%20the%20subject%3F%20I%20want%20to%20know%20before%20removing%20a%20mail%20contact%20if%20it%20is%20a%20member%20of%20a%20Distribution%20group.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20Regards%2C%3C%2FP%3E%3CP%3EMark%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-239398%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EIdentity%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESecurity%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-388712%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20mailcontact%20if%20member%20of%20any%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-388712%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3Ethe%20need%20for%20speed%3F%20try%20this...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EGet-ADObject%3C%2FSPAN%3E%3CSPAN%3E%3CSPAN%3E%3CFONT%20face%3D%22Lucida%20Console%22%3E-LDAPFilter%3C%2FFONT%3E%3C%2FSPAN%3E%3CSPAN%3E%3CFONT%20face%3D%22Lucida%20Console%22%3E%22(%26amp%3B(objectCategory%3Dcontact)(objectClass%3Dcontact)(memberOf%3D*))%22%3C%2FFONT%3E%3C%2FSPAN%3E%3CSPAN%3E%3CFONT%20face%3D%22Lucida%20Console%22%3E-Properties%3C%2FFONT%3E%3C%2FSPAN%3E%3CFONT%20face%3D%22Lucida%20Console%22%3E%3CSPAN%3EName%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CSPAN%3EMemberOf%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CSPAN%3ECanonicalName%3C%2FSPAN%3E%3C%2FFONT%3E%3CSPAN%3E%3CFONT%20face%3D%22Lucida%20Console%22%3E%7C%3C%2FFONT%3E%3C%2FSPAN%3E%3CSPAN%3E%3CFONT%20face%3D%22Lucida%20Console%22%3Eft%3C%2FFONT%3E%3C%2FSPAN%3E%3CFONT%20face%3D%22Lucida%20Console%22%3E%3CSPAN%3EName%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CSPAN%3ECanonicalName%3C%2FSPAN%3E%3CSPAN%3E%2C%3C%2FSPAN%3E%3CSPAN%3Ememberof%20-wrap%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-239778%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20mailcontact%20if%20member%20of%20any%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-239778%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20following%20will%20get%20you%20a%20cleaner%20list%20with%20all%20mailboxes%20followed%20by%20contacts%20%24m%20%3D%20New-Object%20System.Collections.ArrayList%20%24c%20%3D%20New-Object%20System.Collections.ArrayList%20Get-DistributionGroupMember%20GroupName%20%7C%20ForEach-Object%20%7B%20if(%24_.RecipientType.ToString().Contains(%22Mailbox%22)%20-eq%20%24true)%20%7B%20%24m.Add(%24_.Name)%20%7D%20else%20%7B%20if%20(%24_.RecipientType.ToString().Contains(%20%22Contact%22)%20-eq%20%24true)%20%7B%20%24c.Add(%24_.Name)%20%7D%20%7D%20%7D%20%24m%20%7C%20Get-Mailbox%20%7C%20ft%20Name%2CFor*%20%24c%20%7C%20Get-MailContact%20%7C%20ft%20Name%2CFor*%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgetappvalley.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgetappvalley.com%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fvidmate.onl%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fvidmate.onl%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftweakbox.mobi%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ftweakbox.mobi%2F%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-239765%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20mailcontact%20if%20member%20of%20any%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-239765%22%20slang%3D%22en-US%22%3E%3CP%3EHere's%20a%20fast%20way%20to%20do%20it%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%24dn%20%3D%20(Get-MailContact%20your_mail_contact).DistinguishedName%3C%2FP%3E%0A%3CP%3EGet-Recipient%20-Filter%20%22Members%20-eq%20'%24dn'%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20will%20return%20all%20DGs%2C%20mail-enabled%20SGs%20and%20O365%20Groups%20the%20contact%20might%20be%20a%20member%20of.%20And%20since%20it's%20a%20server-side%20filter%2C%20you%20don't%20need%20to%20iterate%20over%20each%20group.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-239680%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20mailcontact%20if%20member%20of%20any%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-239680%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20would%20need%20a%20script%20that%20cycles%20through%20all%20the%20distribution%20groups%20and%20members%20within%20them%20and%20list%20the%20groups%20that%20a%20particular%20member%20is%20a%20part%20of.%20I%20got%20this%20task%20back%20in%20the%20days.%20you%20can%20use%20this%20script%20to%20check%20if%20a%20user%20is%20a%20part%20of%20a%20distribution%20group.%26nbsp%3B%3C%2FP%3E%3CH5%20id%3D%22toc-hId-1058082487%22%20id%3D%22toc-hId-1228847102%22%20id%3D%22toc-hId-1228847102%22%20id%3D%22toc-hId-1228847102%22%3E%3CA%20title%3D%22Get%20Distribution%20lists%20that%20a%20member%20is%20a%20part%20of%22%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2Fscriptcenter%2FGet-Distribution-lists-2ceda593%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGet%20Distribution%20lists%20that%20a%20member%20is%20a%20part%20of%3C%2FA%3E%3A%26nbsp%3B%3C%2FH5%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgallery.technet.microsoft.com%2Fscriptcenter%2FGet-Distribution-lists-2ceda593%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgallery.technet.microsoft.com%2Fscriptcenter%2FGet-Distribution-lists-2ceda593%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-239525%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20mailcontact%20if%20member%20of%20any%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-239525%22%20slang%3D%22en-US%22%3E%3CP%3EHey%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F165011%22%20target%3D%22_blank%22%3E%40Mark%20Louie%20Diaz%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20think%20this%20would%20work%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24Username%20%3D%20%22user%40domain.com%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22crayon-v%22%3E%24DistributionGroups%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-o%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-r%20%22%3EGet-DistributionGroup%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-o%22%3E%7C%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-st%22%3Ewhere%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-sy%22%3E%7B%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-sy%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-r%20%22%3EGet-DistributionGroupMember%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-v%22%3E%24_%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-sy%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-i%22%3EName%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-o%22%3E%7C%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-st%22%3Eforeach%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-sy%22%3E%7B%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-v%22%3E%24_%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-sy%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-i%22%3EPrimarySmtpAddress%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-sy%22%3E%7D%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-sy%22%3E)%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-cn%22%3E-contains%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-s%22%3E%22%24Username%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22crayon-sy%22%3E%7D%3CBR%20%2F%3E%3C%2FSPAN%3E%3CBR%20%2F%3EJust%20toss%20in%20the%20email%20address%20for%20the%20mail%20user%20in%20there%2C%20and%20it%20should%20return%20any%20DL%20that%20has%20that%20user.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAdam%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-905597%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20mailcontact%20if%20member%20of%20any%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-905597%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHey%20Vasil%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOn%20this%20here%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24dn%20%3D%20(Get-MailContact%20your_mail_contact).DistinguishedName%3C%2FP%3E%3CP%3EGet-Recipient%20-Filter%20%22Members%20-eq%20'%24dn'%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20would%20i%20add%20an%20input%20file%20to%20your%20script%3F%20for%20instance%20i%20would%20like%20to%20run%20the%20above%20cmdlets%20on%20a%20list%20of%20122%20people.%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERobert%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1151099%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20mailcontact%20if%20member%20of%20any%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1151099%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20working%20on%20the%20same%20ask.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F165011%22%20target%3D%22_blank%22%3E%40Mark%20Louie%20Diaz%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDid%20you%20find%20a%20resolution%3F%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Contributor

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

7 Replies
Highlighted

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

Highlighted

You would need a script that cycles through all the distribution groups and members within them and list the groups that a particular member is a part of. I got this task back in the days. you can use this script to check if a user is a part of a distribution group. 

Get Distribution lists that a member is a part of

https://gallery.technet.microsoft.com/scriptcenter/Get-Distribution-lists-2ceda593

Highlighted

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.

Highlighted

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/

@Vasil Michevthe need for speed? try this...

 

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

Highlighted

@Vasil Michev 

 

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

 

Highlighted

I am working on the same ask. @Mark Louie Diaz 

Did you find a resolution?