Powershell script to search mailbox using multiple attributes RRS feed

  • Question

  • Hi,

    i am looking to search few mailboxes and delete few emails based on sender and subject. I wrote below script and it works well with just Subject or sender however when i tried with combination of both, it fails. Any help on this would be really helpful. 


    $Users = import-csv -path "C:\EmailSubject.csv"

    foreach ($User in $users)

    $Subject = $User.EmailSubject
    $SenderAddr = $User.Senderaddress
    $MailboxName = $User.recipientaddress
    get-mailbox -Identity $MailboxName | Search-Mailbox -SearchQuery ('(from:"$SenderAddr") AND (subject:"$Subject")') -deletecontent

    Write-host "Deleted $Subject email from" $User.recipientaddress


    Thanks, Karthikeyan R

    Thursday, August 31, 2017 8:30 AM


  • get-mailbox $MailboxName | 
    	Search-Mailbox -SearchQuery "from:$SenderAddr AND subject:$Subject" -deletecontent


    • Marked as answer by Karthikeyan44 Thursday, August 31, 2017 10:38 AM
    Thursday, August 31, 2017 8:37 AM

All replies