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
# 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
# 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
# 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
# Do/while loop for paging through the folder
# 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
# Load the headers using the property set defined above
# Display the headers - using a little foreach loop, displaying them in the normal format
# Set the offset to it's current value plus the page size
} while ($findResults.MoreAvailable) # Do/While loop will continue when more results are available