header image

Outlook: Viewing mail items

Posted by: | August 4, 2011 | No Comment |

Continuing our perambulation around Outlook when used with multiple hotmail accounts its time to look at the other folders and the mail items they contain. This post will show how to view the mail items and a future post will show how to delete items from a specific folder.

Viewing mail items cam be performed with the following function

function get-mailitem {            
param (            
$outlook = New-Object -ComObject Outlook.Application            
$folders | where {$_.Path -like "*$mailfolder"} |            
foreach {            
  $targetfolder = $outlook.Session.GetFolderFromID($_.EntryID, $_.StoreID)            
  if ($all){            
  else {            
   $targetfolder.Items | sort  SentOn -desc | select SenderEmailAddress, Subject, SentOn            

The function takes a mandatory string to identify the mail folder and we use the GetFolderFromID method to access the folder. If the –all switch is used we get a dump of the full object for each mail item otherwise a summary consisting of the sender’s address, date sent and subject is output.

If the –all switch is used we can use the PowerShell pipeline to perform further processing e.g.

get-mailitem –mailfolder Baen –all | where {$_.SentOn –lt [datetime]”1 January 2007”}

under: Office 2010, Outlook, PowerShellV2, Windows 7