PowerShell and Outlook – Part 2

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:

$InBox.Items.Sort("[ReceivedTime]",$true)

Once again $InBox.Items[1] 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[1] 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

$Items[1].PrintOut()

Save the message

$Items[1].SaveAs($path,$type)

Delete the message

$Items[1].Delete()

and to complete the circle… Display the message in a standard Outlook message window.

$Items[1].Display()

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.

Advertisements
This entry was posted in PowerShell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s