3

I am trying to read my Outlook e-mail with the subject line "Automation" and process further with custom script. The below script reads the e-mail with the subject line but it reads the entire count of e-mail with the subject "Automation".

I want to be able to read only the most recent e-mail and process only that specific e-mail content and mark the e-mail as unread. And then read the next new e-mail with the same subject and process only the specific content.

Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$folder.items | where { $_.subject -match 'Automation' } | Select-Object -Property body

Let's say if I have 10 new e-mail with subject "Automation" process 10th e-mail and mark as read and continue to process from 9 to 1.

How to achieve this?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Karthi Keyan
  • 47
  • 1
  • 5

1 Answers1

3

Just got to use a foreach-object, you can mark the mail as read/unread by modifying the unread property of a mail item ( https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook._mailitem.unread.aspx )

$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$folder=$namespace.GetDefaultFolder(6)
$folder.Items | 
    ?{$_.subject -match "automation" } |
    sort receivedtime -desc | 
    %{
         echo $_.body #do stuff with body 
         $_.Unread=$false #mark as read        
     }

After your comment you can verify if your outlook version expose the unread property with : $folder.Items |select -first 1 | get-member you should find the following property :
UnRead Property bool UnRead () {get} {set}

Loïc MICHEL
  • 24,935
  • 9
  • 74
  • 103
  • I run below script Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null $olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type] $outlook = new-object -comobject outlook.application $namespace = $outlook.GetNameSpace("MAPI") $folder = $namespace.getDefaultFolder($olFolders::olFolderInBox) $folder.Items | ?{$_.subject -match "automation" } | sort receivedtime -desc | Select-Object -property Body, subject -First 1 | %{ echo $_.body #do stuff with body $_.Unread=$false #mark as read } – Karthi Keyan Nov 17 '17 at 13:26
  • Received error [ Exception setting "Unread": "The property 'Unread' cannot be found on this object. Verify that the property exists and can be set."] – Karthi Keyan Nov 17 '17 at 13:28
  • I'm using office2016 if matters... i'll update my answer with complete code – Loïc MICHEL Nov 17 '17 at 14:41
  • Hi Loic, I get the property on my Outlook $folder.Items |select -first 1 | get-member you should find the following property : UnRead Property bool UnRead () {get} {set} but I still get the same error. Pls help – Karthi Keyan Nov 18 '17 at 04:22