Searching Outlook with PowerShell

So, I was in the office this afternoon and had to pull some stats from email. I had a few options but the most interesting option was to work out how to scan a set of mails for specific information.

The data I wanted to get was: Title and Send Date. The script I used is below:

$olFolderInbox = 6 #the number 6 indicates Inbox
$ol = new-object -comobject “Outlook.Application”
$mapi = $ol.getnamespace(“mapi”)
$inbox = $mapi.GetDefaultFolder($olFolderInbox)
$msgs = $inbox.Folders.Item(“work”).Folders.Item(“bla”) #these are the folders I am searching in

foreach ($msgs in $inbox.Folders.Item(“work”).Folders.Item(“bla”))
{
$msgs.items | Select-Object Subject,  senton | export-Csv d:\emails.csv -noTypeInformation
}

To search in different folders use the following CONST:

      • olAppointmentItem = 1
      • olFolderDeletedItems = 3
      • olFolderOutbox = 4
      • olFolderSentMail = 5
      • olFolderInbox = 6
      • olFolderCalendar = 9
      • olFolderContacts = 10
      • olFolderJournal = 11
      • olFolderNotes = 12
      • olFolderTasks = 13
      • olFolderDrafts = 16

There’s a lot of useful information available , if you want to get a sample of detail run the below script and it’ll get the data of 5 mails

$sentMail = $outlook.Session.GetDefaultFolder(5) # == olFolderSentMail
$sentMail.Items | select -first 5 TaskSubject

Advertisements
This entry was posted in Uncategorized. 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