How would I go about using powershell to return the text and headers of the last 5 messages received to my exchange email account? Is there a simple way/library to do this?
This is related to my question about not using outlook on superuser. Except that having not found any good alternatives I figure I might as well write my own simple powershell client.
You'll need to have the EWS API installed, and you'll need to check the path to the DLL in the Reflection Assembly load portion.
This should get you to the point where you're able to work with the $inbox.FindItems(5) statement and filter the results you want out of that.
Firstly, apologies that this reply is nearly two years after the question, but I also wanted to check email using Powershell and found this question. Hopefully my code will serve as a reference/starting point for someone else looking to mine outlook from Powershell. I plan to enhance this myself to make it more usable.
I'm pretty new to Powershell, so my scripts are predominantly Frankenstein-ed from various articles, blog posts and StackOverflow Q&A's of course, the script below being no exception!
Following on from Chris' response, I did a little further digging around the internet and cobbled together a few snippets of Powershell to allow me to display a few key pieces of information from emails.
It's sadly lacking in any 'proper' style and I'm sure that any Powershell gurus will cringe at this. But what this code does do is
FindItems
method doesn't return the full mail item, you have to make another round-trip to get the extra information you need.To use...
Download the EWS from here then extract somewhere, e.g.
msiexec /a C:\Path\To\Downloads\EwsManagedApi.msi /qb TARGETDIR=C:\Progs\EwsManagedApi
then call this script using dot-source, e.g.
. C:\Path\To\Script\Outlook_ReadInbox.ps1
which allows you to reference the objects/variables from the script after it has executed.
The code has a limited comments throughout, as well as a few links at the end, which I referenced when cobbling the script together.
Here's my alpha draft of code to read in the first 5 emails, display whether read/unread and show the first 100 characters of the email body on one line with white-space removed.