Powershell MS Outlook - When you process your inbox does it process all the mails? RRS feed

  • Question

  • #connect to outlook
    $GetOutlook = New-Object -com "Outlook.Application";
    $olName = $GetOutlook.GetNamespace("MAPI")
    $olxEmailFolder = $olName.GetDefaultFolder(‘olFolderInbox’)


    $items = $olxEmailFolder.items
    $filter = "[ReceivedTime] > '#07/11/2013#'"
    $inbox = $namespace.GetDefaultFolder($olFolders::olFolderInbox).Items.Restrict($filter)

    The script takes much time to complete. Does it means that it processes all the mails in my Inbox?

    My Inbox contains more than 18000 mails.

    I want to process the mails received today, but the script takes long time to complete.

    Please advise.

    • Moved by Bill_Stewart Wednesday, August 21, 2013 4:11 PM Move to more appropriate forum
    Wednesday, July 10, 2013 8:27 PM

All replies

  • Yikes 18000 emails?  It is filtering, but not all the way to the left.  It is checking against the entire inbox, so it will take a while with that many emails in there.  I have 217 items in my inbox and it finds the 14 from today in about 0.2 seconds.  If archiving isn't an option you'll want to limit the number of emails you're filtering against.

    I noticed it didn't work for me as is, but after I changed the line from $inbox = $namespace.GetDefaultFolder($olFolders::olFolderInbox).Items.Restrict($filter) to $inbox = $items.Restrict($filter) it returned the expected results.   also 07/11/2013 hasn't occured yet (for me), so I changed it to 07/10/2013.

    For it to process faster you might want to grab just the latest ### emails that would be in the general area of what you're looking for and then filter off of that.  I'm not getting the exact syntax right, but something like:

    $olxEmailFolder = $olName.GetDefaultFolder('olFolderInbox').Items.GetFirst(500)

    then when you get the count of items filtered for today it would be something like 400 out of 500 instead of 400 out of 18000.  I'm just not sure what syntax it's looking for.  For me, that returns an error "Cannot find an overload for "GetFirst" and the argument count: "1".

    I'll keep an eye on this thread because this is pretty interesting!


    • Edited by -Nick Wednesday, July 10, 2013 9:29 PM
    Wednesday, July 10, 2013 9:27 PM
  • I'm not an Outlook/Exchange expert, but I would also suspect that if you are not running Outlook in cached Exchange mode this would be a lot slower. But in any case I think that this thread is probably more suited to the Outlook for Developers forum.


    Wednesday, July 10, 2013 9:54 PM
  • This is why we tell users to move mail ot of the inbox.  It can improve overall performance.

    Post in Exchange forum for assistance.  It is not - as has been pointed out - a scripting issue.


    Wednesday, July 10, 2013 10:22 PM
  • Thanks for reply Nick.

    I tried

    $olxEmailFolder = $olName.GetDefaultFolder('olFolderInbox').Items.GetFirst(500)

    but got the same error as yours

    Cannot find an overload for "GetFirst" and the argument count: "1".

    Thursday, July 11, 2013 7:38 PM
  • olFolderInbox is a number and not a string.


    Thursday, July 11, 2013 7:42 PM
  • $ol=New-Object -com Outlook.Application

    Get-First takes no arguments.  It just gets the first item that can pass the filter.

    Once you have overused you inbox there is nothing you can do. The items will be filtered efficiently on indexed properties. Subject is not indexed.

    Retrieving items via automation will be significantly slower than in Outlook or Exchange.


    Thursday, July 11, 2013 7:52 PM
  • right.  Unfortunately the GetFirst method only returns the first item and not a number of items (as the -First parameter does in many powershell cmdlets)

    see MSDN for more info:

    Perhaps someone with more outlook/developer experience can provide a way to filter further to the left.  The best option seems to be limiting what's in your inbox, and then the query will complete in a reasonable amount of time.

    • Edited by -Nick Thursday, July 11, 2013 8:12 PM
    Thursday, July 11, 2013 7:52 PM
  • As I have repeatedly told my clients over the years.  DO NOT LEAVE EVERYTHING IN YOUR INBOX.

    It is a lesson that some will never learn.  There is no way out of this.  A search is a search.

    In Outlook 2010 and later you can add desktop search and index the inbox.  This is extremely fast once it is indexed.


    Thursday, July 11, 2013 8:10 PM
  • Thanks Nick and jrv for reply.

    I will try to control inbox mail count.

    I was developing a robust script so that everybody in my team can use it without any specific folders.

    I have a folder specific script and it completes in 2-3 minutes.

    Thank you again for your help and tips.

    Saturday, July 13, 2013 11:27 PM