get unread mail on outlook and set it to read

Welcome Forums General PowerShell Q&A get unread mail on outlook and set it to read

This topic contains 5 replies, has 2 voices, and was last updated by

 
Participant
2 years ago.

  • Author
    Posts
  • #56287

    Participant
    Points: 1
    Rank: Member

    Selecting email in outlook:

    $OL=New-Object -ComObject OUTLOOK.APPLICATION
    $ns =$OL.GETNAMESPACE("MAPI")
    $folder=$ns.pickfolder()
    $mail=$folder.Items.Restrict('[UnRead] = True') 
    

    Retrieving 8 mails, checked the $mail. There all in there.
    (restrict is very nice here, tried getting all the mail and then loop though them so see if there unread, but is takes forever)

        ForEach ($Item in $mail) { $item.unread }  
    

    I only does 4, repeating is a few times is setting more mails on unread=false.
    But is should work in 1 go, anyone knows whats happening?

  • #56458

    Participant
    Points: 1
    Rank: Member

    No one ?

  • #56462

    Participant
    Points: 4
    Rank: Member

    This is odd behaviour.

    I found this post which inspired the code below. It works but it feels wrong.

    $OL = New-Object -ComObject OUTLOOK.APPLICATION
    $ns = $OL.GETNAMESPACE("MAPI")
    $folder = $ns.pickfolder()
    
    $mail = $folder.Items.Restrict('[UnRead] = True') 
    
    while ($mail.count -gt 0) {
    
        foreach ($item in $mail) {
          
            $item.UnRead = $false
    
        }
    
        $mail = $folder.Items.Restrict('[UnRead] = True') 
    
    }
    
    
  • #56465

    Participant
    Points: 1
    Rank: Member

    Thanks.

    $mail = $folder.Items.Restrict('[UnRead] = True') 

    This is geeting the mail again and again until everything is processed.
    Just like i said, if you keep getting the mail, finaly it works.
    Only i did i manualy, still dont understand what is happening.

  • #56470

    Participant
    Points: 4
    Rank: Member

    I've done a bit more digging. It looks like the $mail collection is dynamic: when you mark an item as read it's removed from the collection. This is affecting the indexing of the collection.

    You can work around this by working backwards through the collection:

    $OL = New-Object -ComObject OUTLOOK.APPLICATION
    $ns = $OL.GETNAMESPACE("MAPI")
    $folder = $ns.pickfolder()
    
    $mail = $folder.Items.Restrict('[UnRead] = True')
    
    for ($i = $mail.count; $i -gt 0; $i--) {
    
        $mail.Item($i).UnRead = $false
    
    }
    
  • #56495

    Participant
    Points: 1
    Rank: Member

    Thanks! That works like a charm.

The topic ‘get unread mail on outlook and set it to read’ is closed to new replies.