Share via


How to get contacts from Active Directory's Distribution Group in CSV using PowerShell?

Question

Tuesday, September 10, 2013 4:54 AM

 Hello,

Please let me know the script, which can be useful for me to extract Contacts from Distribution Group of Active Directory to CSV using PowerShell.

Thanks,

Divyaprakash Koli

All replies (9)

Tuesday, September 10, 2013 5:46 PM âś…Answered | 3 votes

Ok,

Note : Contact does not contain the SamAccountName because this not contain AD login credential.

use this script ,it will shows  what you want :

$GroupName=Read-Host "Enter the name of Group for Which you want to Export Contacts"

$memberof=get-adgroup $GroupName |select -expandproperty distinguishedname

Get-ADObject -Filter 'memberof -eq $memberof -and (objectClass -eq "user" -or ObjectClass -eq "contact")' -properties *|select name,Samaccountname,Objectclass #|Export-csv  Contacts.csv -NoTypeInformation

I have insert # before Export-csv , once you get the desired output on your screen , you can remove # and  then run this script ,it will export in a .csv file.

Here is output when I ran this script :

PS C:\ .\contact.ps1
Enter the name of Group for Which you want to Export Contacts: contact_group

name                                 Samaccountname             Objectclass
                                                             
user5                                   user5                                   user
user6                                   user6                                   user
user7                                   user7                                   user
user8                                   user8                                   user
user9                                   user9                                   user
contact1                                                                        contact
contact2                                                                        contact

If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2


Tuesday, September 10, 2013 7:30 AM

Can you explain your requirement ?

If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2


Tuesday, September 10, 2013 9:39 AM

I assume , You have a AD group say "ADContacts "  and there are multiple contacts and AD users are member of this AD group  and you want to extract all contacts only  from this group in a .csv file.

If so ,  this script can do the task for you .

$GroupName=Read-Host "Enter the name of Group for Which you want to Export Contacts"

$memberof=get-adgroup $GroupName |select -expandproperty distinguishedname

Get-ADObject -Filter 'objectclass -eq "contact" -and memberof -eq $memberof' -properties *|select name,mail,@{e={"$($_.memberof)"};l="Member Of"}|Export-csv  Contacts.csv -NoTypeInformation

Hope it helps you.

If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2


Tuesday, September 10, 2013 3:00 PM

Hello Tarique,

Output of CSV is Blank.

What I am getting with my script is only "Users from particular Group" by using -

"

$Group = read-host "`nPlease Enter Group Name"

Get-ADGroupMember $Group | Select-Object -Property name,SamAccountName,objectClass | Export-CSV C:\GroupMember.csv

"

Please make some modifications so I can get Contacts too.

Thanks,

Divyaprakash Koli


Tuesday, September 10, 2013 3:06 PM

Remove the Export-Csv cmdlet. Do you get output? If not, remove the select command as well.

Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,610+ strong and growing)


Tuesday, September 10, 2013 3:25 PM

Hello Tarique,

Output of CSV is Blank.

What I am getting with my script is only "Users from particular Group" by using -

"

$Group = read-host "`nPlease Enter Group Name"

Get-ADGroupMember $Group | Select-Object -Property name,SamAccountName,objectClass | Export-CSV C:\GroupMember.csv

"

Please make some modifications so I can get Contacts too.

Thanks,

Divyaprakash Koli

have you try my script ?

My script is working without any issue ........ and I am getting the Contacts in CSV file.

I can see that  you have change my code   .........

If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2


Tuesday, September 10, 2013 3:36 PM

What's your requirement ?

Explain in details .

If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2


Tuesday, September 10, 2013 4:52 PM

Hello,

I wish to get "users and contacts" (only contacts will also be helpful) of particular Group (Distribution or Security) in to CSV with attribute "name, samaccountname, Objectclass".

Thanks,

Divyaprakash Koli


Wednesday, September 11, 2013 2:56 AM

Hello Tarique,

Above solution does not work well in terms of 'Users' as it shows very few users as compared to my script "Get-ADGroupMember" and don't know why so.

But Anyway, I am happy for 'Contact', which I needed.

Thanks for your time and efforts.

Thanks,

Divyaprakash Koli