Powershell: Exporting Active Directory Contacts

Some time ago, I needed to have a list of all Contacts registered in Active Directory. Knowing that there are a lot of them (numbering at least eighty), getting the data manually was not a viable alternative, particularly knowing that the same objective can be achieved through Powershell. I eventually came up with a solution. To make following it logically easier, I’m going to include commentary on each step:

  • First, we need to retrieve Active Directory objects. This is done with the cmdlet Get-ADObject
  • Next, we need to filter those results, based on the type of object. This is done with -LDAPFilter "objectClass=contact"
  • Running this straight will not include email addresses. In order to get those, we add -Properties "mail"
  • I wanted this exported as a file, which is done by the final part of the script: | Export-CSV filename.csv

All together, the final script looks like this:

Get-ADObject -LDAPFilter "objectClass=contact" -Properties "mail" | Export-CSV filename.csv

When testing it, I noted that each result was listed neatly with Distinguished Name, e-mail address, name, ObjectClass and ObjectGUID in separate rows. In order to get an export that was more readily legible, I replaced Export-CSV with Out-File fillename.txt, which dumped the result to a text file in much the same way as using > would in the Windows command line. That script looks like this:

Get-ADObject -LDAPFilter "objectClass=contact" -Properties "mail" | Out-File filename.txt


Posted

in

,

by

Comments

By posting a comment, you consent to our collecting the information you enter. See privacy policy for more information.

This site uses Akismet to reduce spam. Learn how your comment data is processed.