How to export Email Headers from Exchange using Powershell

This one’s just a quick post demonstrating how to simply extract data using Exchange Web Services’ Managed API via Powershell. Using the EWS Managed API can at first appear scary, especially if you don’t have experience with C#; however once you’ve done it a few times you’ll begin to enjoy it..

In one of my previous posts, I cover how to download and install the managed API before using a contacts import script, so I won’t go over it again. What this script does, is connect to the currently logged on users’ mailbox, and output as plain text From, Subject, References, Message ID and Headers for each message in the inbox.


# Change $mail to your email address
$mail="you@domain.com"
# Set the path to your copy of EWS Managed API
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"
# Load the Assemply
[void][Reflection.Assembly]::LoadFile($dllpath)
# Create a new Exchange service object
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService
# Autodiscover using the mail address set above, using the logged on user's credentials
$service.AutodiscoverUrl($mail)
# The Pagesize is used to split the EWS requests up into easily digestable parts for large folders
$pagesize = 100
# Offset keeps track of how for we are along a large folder. Set to 0 initially.
$offset = 0
# Create a property set that will allow us to pull out the message headers, as they aren't returned by default
$propertySet=new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::InternetMessageHeaders)
# Do/while loop for paging through the folder
do
{
# Set what we want to retrieve from the folder
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($pagesize,$offset)
# Retrieve the data from the folder
$findResults = $service.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$view)
# The For Each loop goes through the items in the results one by one
foreach ($item in $findResults.Items)
{
# Output the results - first of all the From, Subject, References and Message ID
"From: $($item.From.Name)"
"Subject: $($item.Subject)"
"References: $($item.References)"
"InternetMessageID: $($item.InternetMessageID)"
"InternetMessageHeaders"
# Load the headers using the property set defined above
$item.Load($propertySet)
# Display the headers - using a little foreach loop, displaying them in the normal format
$item.InternetMessageHeaders|foreach{"$($_.Name): $($_.Value)"}
""

}
# Set the offset to it's current value plus the page size
$offset+=$pagesize
} while ($findResults.MoreAvailable) # Do/While loop will continue when more results are available

5 thoughts on “How to export Email Headers from Exchange using Powershell

  1. Pingback: Reading emails from Office365 account using PowerShell | sysadminben

  2. Steve,

    great blog and great script. I’ve been looking for something like this and tried to implement it in my Exchange 2010 environment. Unfortunately I have to parse the email headers of all emails in a certain user’s mailbox (including subfolders). Why? Because I need to find out whether “Disposition-Notification-To” in the email header is set to TRUE. Therefore I tried to adjust your script in order to receive information on $item.IsReadReceiptRequested. The script works so far as long I’m logged into my Exchange/PowerShell mit admin priviliges, i.e. my Administrator mailbox will be parsed.

    When trying to impersonate the targeted user (let’s just call him Paul) by entering the corresponding email adress $mail=paul@domain.com it doesn’t change anything. My admin mailbox still gets parsed though $mail had been adjusted. EWS API 1.2 has been installed and works. I tried to impersonate but failed. I’m pretty knew to impersonating and was wondering whether you have some advice. I had a look at all the links you provided but to no prevail.

    I executed the following commands in PS in order to enable impersonation:

    New-ManagementRoleAssignment -Name:impersonationAssignmentName -Role:ApplicationImpersonation -User:administrator

    Add-ADPermission -Identity “Paul” -User administrator -extendedRight ms-Exch-EPI-May-Impersonate

    Any help would be greatly appreciated.
    Alex

  3. I tried this on exchange 2010 sp1 and it bombs out at :

    $item.Load($propertySet) with an error saying I cannot call a method on a null-valued expression.

  4. Pingback: How to export Email Headers from Exchange using Powershell | Steve … | www.erasedmail.com

Leave a Reply