Getting Contacts from user mailbox using EWS managed API

In my previous posts on EWS we worked on how to move, remove, export emails items, in this post I will demonstrate how we can work with user Contacts and export the contact in CSV or fetch the contact details.
If you are following my previous post then you would know we just need to access the user Contacts folder and load the items in it with the property set and then get the data we need.

$ContactsFolderid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
$items = $service.FindItems($ContactsFolder.ID,$view)
$service.LoadPropertiesForItems($items, [Microsoft.Exchange.WebServices.Data.PropertySet]::FirstClassProperties)

Most of the data is easily available when it comes to contacts but there are few attributes where we needs to go into the little deep.
So let me talk about how do we work with these attribute and get the data from each member.
Below is the snap shot of a single contact.
You can get it from collection using below method and then check what all field you need and then add the same into the table.

So lets see how we get the data from the attribute with Dictionary object.
If you do “$items.Items[0] | get-member” on any dictionary object you will be able to see the method and ParameterizedProperty.

The best way to get the member for the item is just put the highlighted string in google and check the msdn link for detailed member list.
Like below link for IMAddress

Now you know the member key name we can fetch the data from the member.

The same way you can fetch data from other attributes, below I have put a PowerShell script for full code review, I hope you find this post informative, your feedback and suggestions are welcome in comment section.

param (

$uri=[system.URI] ""
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Import-Module $dllpath

## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $userName, $password
$service.url = $uri
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId `

$ContactsFolderid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList $ItemsView
$items = $service.FindItems($ContactsFolder.ID,$view)

[void]$service.LoadPropertiesForItems($items, [Microsoft.Exchange.WebServices.Data.PropertySet]::FirstClassProperties)

foreach ($item in $items.items) {

$cValues = "" | Select FileAs,DisplayName,GivenName,Surname,CompanyName,EmailAddresses,OfficeLocation,PhysicalAddresses, `

$cValues.FileAS = $item.FileAs
$cValues.DisplayName = $item.DisplayName
$cValues.GivenName = $item.GivenName
$cValues.Surname = $item.Surname
$cValues.CompanyName = $item.CompanyName
$cValues.EmailAddresses = $item.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Address
$cValues.OfficeLocation = $item.OfficeLocation
$cValues.PhysicalAddresses = $item.PhysicalAddresses[[Microsoft.Exchange.WebServices.Data.PhysicalAddressKey]::Business].City
$cValues.PhoneNumbers = $item.PhoneNumbers[[Microsoft.Exchange.WebServices.Data.PhoneNumberKey]::MobilePhone]
$cValues.Department = $item.Department
$cValues.JobTitle = $item.JobTitle
$cValues.ImAddress = $item.ImAddresses[[Microsoft.Exchange.WebServices.Data.ImAddressKey]::ImAddress1]