none
Powershell - Create Search Folders RRS feed

  • Question

  • Hi,

    I'm new to Powershell and VBSscrip scripting. I'm trying to create a couple of search folders with certain criteria. I have created these search folders manually to see what they should contain. 

    1. X_Received folder looks for email from external emails. The criteria is FROM:@. This will search in Inbox, sent items, archive, and a custom folder called Folders
    2. X_Sent folder looks for emails sent to external emails. The criteria is FROM:[displayname] and TO:@. This will search in sent Items and the custom folder "Folders"

    Below is just a small sample of my code

    $Outlook=New-Object -ComObject OUTLOOK.APPLICATION
    
    # x_Received
    [string]$rscope = "'Inbox','Sent Items', 'Archive', 'Folders'"
    [string]$rFilter = "http://schemas.microsoft.com/mapi/proptag/0x0C1F001E LIKE '%@%'"
    $rsearch = $Outlook.AdvancedSearch($rscope, $rFilter, $true, 'This is a test')
    $rsearchFolder = $rsearch.Save("xReceived_Retention")
    
    # x_Sent
    [string]$sscope = "'Sent Items','Folders'"
    [string]$sFilter = "http://schemas.microsoft.com/mapi/proptag/0x0042001F Like '%$username%' 
                        AND http://schemas.microsoft.com/mapi/proptag/0x39FE001F LIKE '%@%'"
           
    $ssearch = $Outlook.AdvancedSearch($sscope, $sFilter, $true, 'Test')
    $ssearchFolder = $ssearch.Save("xSent_Retention")

    The x_Received retrieves the current user ($username) and it creates the folder as expected. However, I'm having trouble creating the X_sent folder. I done plenty of research on DASL filtering and determined that http://schemas.microsoft.com/mapi/proptag/0x39FE001F is PR_EMAIL_ADDRESS_W. I've used OutlookSpy to retrieve this information. However, this search folder is empty and doesn't display any emails.

    I don't know how to proceed with this any further. I can use http://schemas.microsoft.com/mapi/proptag/0x0E04001E(PR_DISPLAY_TO") but this filter only works if the Display name in the TO field has an "@". If the email of the external email is displayed with a name, that email is skipped. For example, if the TO field has johnsmith@external.com, it is captured in this filter. However, some external emails have a name like 'John Smith' in the TO field and these are not captured.

    Any assistance would be greatly appreciated

    Thursday, June 7, 2018 1:24 PM

All replies

  • Hi JC_Mene,

    Thanks for visiting our forum, then here we mainly focus on general issues about Outlook client. Since your query is more related to developing issues involving Outlook, I'll move your thread to the dedicated MSDN forum for Outlook for better response. 

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Yuki Sun


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, June 8, 2018 2:23 AM
  • On the Extended MAPI level, you would need to create a subrestriction on the PR_MESSAGE_RECIPIENTS property, and then specify a restriction on the PR_EMAIL_ADDRESS property. AdvancedFind does not do that to the best of knowledge.

    If using Redemption is an option, SQL restriction on Recipients / To / CC / BCC would create a PR_MESSAGE_RECIPIENTS restriction with the sub restriction (OR) on PR_DISPLAY_NAME and PR_EMAIL_ADDRESS.

    You can use RDOSearches object to create a new Outlook search visible to the user.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, June 8, 2018 5:07 AM
  • Hello JC_Mene,

    >>LIKE '%@%'

    What's the criteria of "TO:@"? Which mail do you want to filter?

    As far as I know, PR_DISPLAY_TO is the display name of the receiver.  Since you are filtering the display name which contains "@", as a  result, the mail who's receiver name does not contain "@" will be skipped. 

    Did I misunderstand anything?

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 8, 2018 9:01 AM
  • Thanks for the assist Dmitry. I'll check out Redemption. But just to confirm, is there no other way of generating a search folder with criteria from the recipients folder?

    Terry - I'm just looking to filer for externally sent emails. I'm using the "@" symbol because that's what I used when creating the search folder manually.

    Friday, June 8, 2018 7:56 PM
  • Extended MAPI is the only other option, but it is not accessible from PowerShell - it is C++ or Delphi only.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, June 8, 2018 7:59 PM