A user wanted to use PowerShell to search his contacts for a value in the custom field.
While you don’t need to use PowerShell to search contacts, and can do a more complicated search within Outlook, you will need to use PowerShell or VBA if you want to search for a value in a custom field.
This PowerShell can search custom fields or Outlook's standard fields using the property tag. While that sounds complicated, it's not. You can get the property tags using MFCMAPI and copy the DSAL string you need to use.
To use MFCMAPI to get the proptag:
- From the Session menu, choose Logon.
- Select your profile from the Choose Profile dialog.
- If you don't see True in the Default Store column, click on Default Store column twice to bring it to the top.
- Right click on the default store and choose Open Store.
- Expand Root -Mailbox then expand IPM_Subtree.
- Right click on Contacts and choose "Open contents table"
- Select a contact and look in the lower pane for the field you need. Note: custom fields may only be present on a contact if it has a value.
- Double click on the field to open the dialog and get the property string.
- Select the string and copy.
For custom fields, you can either use the longer DSAL string or use http://schemas.microsoft.com/mapi/proptag/
and add the tag to it.
DASL: http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/txtNotes or proptag: http://schemas.microsoft.com/mapi/proptag/0x89E8001F
The PowerShell is not case sensitive. The query LIKE '%"+$query+"%'"
does a wildcard search, looking for the word anywhere in the field. Remove one % sign to match the beginning or end of the word, or both to return only contacts with that exact word in the field.
To use, open the PowerShell ISE and paste the code into the Script pane at the top. If the script pane is not visible, press Ctrl+1 to show it.
Edit the $query and the $filter strings. Then run the script.
cls Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null $outlook = New-Object -com Outlook.Application; $namespace = $outlook.GetNamespace("MAPI"); $objContacts = $nameSpace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderContacts) $query = "DEF" Register-ObjectEvent -InputObject $outlook -EventName "AdvancedSearchComplete" -Action { Write-Host Write-Host "Search Results for" $query $args.Scope write-host "*** " (get-date) "****" if ($args.Results) { foreach ($result in $args.Results) { write-host $result.Fullname `t $result.email1address `t $result.hometelephonenumber } write-host "*** END ***" #Clears the search results Get-EventSubscriber -Force | Unregister-Event -Force } } Function Get-ContactFields { $scope= ($objContacts.folderpath).replace("\\","\") write-host $scope #contains the query word $filter = "http://schemas.microsoft.com/mapi/proptag/0x89E8001F LIKE '%"+$query+"%'" #is exact #$filter = "http://schemas.microsoft.com/mapi/proptag/0x89E8001F LIKE '"+$query+"'" write-host $filter $search = $outlook.AdvancedSearch("'$scope'", $filter, $True) } Get-ContactFields