In Part 1 we covered some basic Outlook terminology and managed to connect to our Inbox and the Items collection it contains. Unfortunately we couldn’t get the first message in the Inbox by the methods we would expect because the Items collection is not stored in a date order (or in fact any particular order).
We can in fact sort the Items by a large number of different headings, including a variety of dates. The one we’re looking for if we want the most recently arrived message is ‘ReceivedTime’ (as before see MSDN for all of the headings). So we should be able to sort by executing:
Once again $InBox.Items doesn’t give us the most recent item. It turns out that you need to create a variable that points to the Items collection and sort that; executing the Sort method directly on the collection doesn’t give an error, it just doesn’t work. So we should try:
$Items = $Inbox.Items $Items.Sort("[ReceivedTime]",$true)
Now $Items gives us the most recent message. Whew!
Now we have our message, what can we do with it? A non-exhaustive list includes:
Access properties (sender, subject, size, message body (various formats))
Print the message
Save the message
Delete the message
and to complete the circle… Display the message in a standard Outlook message window.
Hopefully that’s enough to get you started. Bear in mind that the Items collection we’ve established contains objects which can be sent down the PowerShell pipeline like any other.
In Part 3 we’ll try to create and send a message.