none
Search Mailboxes with CSV input

    Question

  • Hi,

    I have a CSV file which has the data as below:

    recipient,subject
    administrator@test.com,test4
    abc@test.com,test5

    My requirement is to write a one liner or script so that it gets the recipient address and search for messages with that corresponding subject line and delete them.

    I have tried the below one-liners but the search results are null:(resultant item count says zero)

    import-csv .\test.csv | ForEach-Object {search-mailbox $_.recipient -searchquery 'subject: "$_.subject"' -deletecontent}

    import-csv .\test.csv | ForEach-Object {search-mailbox $_.recipient -searchquery 'subject: $_.subject' -deletecontent}

    Result:

    ResultItemsCount : 0
    ResultItemsSize  : 0 B (0 bytes)

    I have also tried in the below way (by importing the csv to a variable D and then the below query)

    ForEach-Object {Search-Mailbox $d.recipient -SearchQuery 'subject: $d.subject' -DeleteContent}

    But the results are same as above.

    But when I run the query below, it gives me expected result:

    Search-Mailbox abc -SearchQuery 'subject: test5' -DeleteContent

    ResultItemsCount : 1
    ResultItemsSize  : 8.563 KB (8,768 bytes)

    Any one has ideas on how to get this job done by taking the data from CSV file ?

    Wednesday, December 17, 2014 7:02 PM

Answers

  • You need to use subexpression syntax to create the subject search filter:

    "subject: $($_.subject)" 



    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinorModerator Wednesday, December 17, 2014 7:07 PM
    • Marked as answer by Raghu.Ram Wednesday, December 17, 2014 9:58 PM
    Wednesday, December 17, 2014 7:07 PM
    Moderator

All replies

  • You need to use subexpression syntax to create the subject search filter:

    "subject: $($_.subject)" 



    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinorModerator Wednesday, December 17, 2014 7:07 PM
    • Marked as answer by Raghu.Ram Wednesday, December 17, 2014 9:58 PM
    Wednesday, December 17, 2014 7:07 PM
    Moderator
  • Okay, I will try this and let you know.

    Thank you for the prompt response.

    Wednesday, December 17, 2014 9:21 PM
  • It's working, Thank you.....
    Wednesday, December 17, 2014 9:59 PM
  • i am trying to do the same. But for me subexpression doesnt work either.

    $info = Import-csv "C:\users\$date.csv"
    foreach ( $line in $info ) {
                                Search-mailbox -id $line.RecipientAddress -SearchQuery { subject:"$line.Subject" } -LogOnly -TargetFolder Purge -TargetMailbox ***** -LogLevel Full
                                }

    please advice

    Monday, February 11, 2019 6:41 PM
  • Please do not add additional questions to other peoples old and already answered threads. Create one for yourself and set a link to this one if needed.

    And please format your code as code using the code posting tool provided on the edit bar of the post editor.

    Thanks


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH_666 Monday, February 11, 2019 7:01 PM
    Monday, February 11, 2019 7:00 PM