PowerShell: Search for a contact within a mailbox (or multiple mailboxes) No ratings yet.


The attached script shows how to use EWS to search for a particular contact (based on email address) within one or more mailboxes, writing any found occurrences to a log file.  You can process multiple mailboxes in a couple of ways (well, there are probably other alternatives too, but these should cover most requirements!).


Processing mailboxes using Exchange Management Shell

You can use the Exchange Management Shell to retrieve mailboxes and then send to the script.  To do so, you need to run the script from the shell, or import a remote Exchange session into a standard PowerShell session (change the ConnectionUri parameter to an appropriate URL for your system):

$c = Get-Credential
$S = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ex15cas1.hybrid.local/powershell/ -Credential $c
Import-PSSession $S

The mailboxes can then be processed as follows:

$m = Get-Mailbox
$m | ForEach-Object {
  .\Search-Contacts.ps1 -Mailbox $_.PrimarySmtpAddress -ContactEmail Leesa.Barela@nowhere.com -LogFile “c:\temp\log.txt” -impersonate

You can modify Get-Mailbox accordingly to return just a subset of mailboxes (e.g. limit to database, OU, etc.).


Processing mailboxes using a list of mailboxes

The script can also accept a text file containing the list of mailboxes to be processed (one email address per line in the text file):

.\Search-Contacts.ps1 -Mailbox “c:\temp\mailboxes.txt” -ContactEmail Leesa.Barela@nowhere.com -LogFile “c:\temp\log.txt” -impersonate

In the above, mailboxes.txt contains the list of mailboxes.


The log file (in the above examples, this is c:\temp\log.txt) will contain details of any mailbox that had a contact with the given email address.


Script parameters (complete list)


-Mailbox: the SMTP address of the mailbox being searched, or the location of a text file containing a list of mailboxes

-ContactEmail: the email address that we are searching for

-Delete: if specified, any contact found with this email address will be deleted

-Username: the username for authenticating with EWS (if missing, default credentials will be used)

-Impersonate: if specified, use impersonation to access the mailbox.  This is required if you are accessing a mailbox that does not belong to the authenticating account

-EwsUrl: if specified, this will be used for all EWS request (if missing, autodiscover is used to determine the correct URL for the mailbox)

-EWSManagedApiPath: the path to the EWS Managed API – this shouldn’t be needed as the script searches all default install locations.  Only necessary if not in default location.

-IgnoreSSLCertificate: ignore certificate errors (required if you are using self-signed certificates that are not trusted by the local machine)

-AllowInsecureRedirection: Whether to allow insecure redirects when performing autodiscover

-LogFile: search results and actions taken are logged to this file if specified



Please rate this

Leave a Reply

Your email address will not be published. Required fields are marked *