'How can I pull Mail Contact groups for each contact with powershell?

I am trying to create a script to pull contact groups and put them in an excel sheet. Here is what I have so far:

Import-Module ActiveDirectory
Get-ADUser -SearchBase "OU=XXXXX,OU=XXXXX,DC=XXXXXX,DC=DOM" -Filter * | foreach-object                                   {
write-host "User:" $_.Name -foreground green
Get-ADPrincipalGroupMembership $_.SamAccountName | foreach-object {
    write-host "Member Of:" $_.name
}
} | Export-Csv c:\tech.csv

This works for User accounts (except for the export for some reason), but not Contacts.

The script doesn't return anything for the distro groups of contacts when running it. We have a list of contacts that are part of distro groups that we, most likely, don't need anymore. I'm running this from a remote computer through powershell and I figured I could use this script and point it to the specific Contact OU.

I have also tried this from our Exchange servers:

Get-Contact | 
sort-object LastName | 
%{ Get-MailContact $_.DistinguishedName | select Name,Member,PrimarySMTPAddress } 

There doesn't appear to be a "Members" property.



Solution 1:[1]

try this. Not optimal, but it works.

get-distributiongroup | foreach {write-host ---------- $.samaccountname; get-distributiongroupmember -resultsize unlimited $.samaccountname | where {$_.RecipientType -eq "mailcontact"} | ft name}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 A T