locked
Script for Search-Mailbox and the Result RRS feed

  • Question

  • Hello Team

    am looking for the small script to delete the particular email from all the mailboxes in the exchange 2013 environment

    Currently am doing to delete by the below commnad

    Get-mailbox -results Unlimited | search-mailbox –searchquery “Subject:’Virus Name’” –LogLevel Full –Targetmailbox "Test Mailbox" –Targetfolder "FolderName" –DeleteContent

    Am looking for the script which  should ask only for the Subject Name to search and delete 

    Enter the Subject Name for deletion : Virus Name

    Once we enter the Subject name , it should start delete the emails from all the mailboxes

    finally it should give the results for which are the mailboxes are deleted successfully and not successfully in CSV format

    is that achievable ? Please help me 

     

     

    Tuesday, March 8, 2016 3:52 PM

Answers

  • Looks like 

    $Subject = Read-Host “Enter the Subject”

    $Sender = Read-Host “Enter the sender email ID”

    $LoggingMailbox = Read-Host “Mailbox alias to send the Log”

    Get-Content “c:ratishscriptsusers.csv” | Get-Mailbox | Search-Mailbox -SearchQuery Subject:”$Subject”,From:$Sender -LogOnly -LogLevel full -TargetMailbox $LoggingMailbox -TargetFolder “SPAM”

    Source : http://msexchangeguru.com/2015/06/12/search-mailbox/

    Nice article

    • Proposed as answer by Lynn-Li Wednesday, March 9, 2016 7:04 AM
    • Marked as answer by rush2ramki Wednesday, March 9, 2016 11:37 AM
    Wednesday, March 9, 2016 6:42 AM
  • Hi,

    It's great that you have found what you want. And that's what I will tell you. Based on your requirement, I edited that script.

    $Subject = Read-Host “Enter the Subject”
    $LoggingMailbox = Read-Host “Mailbox alias to send the Log”
    $filename = Read-Host "Enter the csv file name"
    Get-mailbox -results Unlimited | search-mailbox –searchquery '$Subject' –LogLevel Full –Targetmailbox "$LoggingMailbox" –Targetfolder "FolderName" –DeleteContent | export-csv C:\$filename.csv

    Best Regards.


    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    Lynn-Li
    TechNet Community Support

    • Marked as answer by rush2ramki Wednesday, March 9, 2016 11:37 AM
    Wednesday, March 9, 2016 7:04 AM

All replies

  • Hi,

    This command is no problem. Since you have TargetMailbox and LogLevel parameters in your command, deleted messages will be copied to the target mailbox and removed from the source mailbox. You can check the results in the target mailbox.

    And try this command, the results in the csv file will tell you if the command is executed successfully and ResultItemsCount

    Get-mailbox -results Unlimited | search-mailbox –searchquery “Subject:’Virus Name’” –LogLevel Full –Targetmailbox "Test Mailbox" –Targetfolder "FolderName" –DeleteContent | export-csv C:\report.csv

    Best Regards.


    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    Lynn-Li
    TechNet Community Support

    Wednesday, March 9, 2016 2:11 AM
  • Hello Lynn Thanks for the update.

    Is there any way to have in script to ask the subject of the email. Once we enter it should start the rest of the process

    like below

    c:\users\ram\:> .\scriptname.ps1

    Enter the Subject name to Search and Delete : Subecjt of the email

    Processing ..........

    Once its completed . we can check the export csv file

    Hope you understand

    

    Wednesday, March 9, 2016 2:43 AM
  • Looks like 

    $Subject = Read-Host “Enter the Subject”

    $Sender = Read-Host “Enter the sender email ID”

    $LoggingMailbox = Read-Host “Mailbox alias to send the Log”

    Get-Content “c:ratishscriptsusers.csv” | Get-Mailbox | Search-Mailbox -SearchQuery Subject:”$Subject”,From:$Sender -LogOnly -LogLevel full -TargetMailbox $LoggingMailbox -TargetFolder “SPAM”

    Source : http://msexchangeguru.com/2015/06/12/search-mailbox/

    Nice article

    • Proposed as answer by Lynn-Li Wednesday, March 9, 2016 7:04 AM
    • Marked as answer by rush2ramki Wednesday, March 9, 2016 11:37 AM
    Wednesday, March 9, 2016 6:42 AM
  • Hi,

    It's great that you have found what you want. And that's what I will tell you. Based on your requirement, I edited that script.

    $Subject = Read-Host “Enter the Subject”
    $LoggingMailbox = Read-Host “Mailbox alias to send the Log”
    $filename = Read-Host "Enter the csv file name"
    Get-mailbox -results Unlimited | search-mailbox –searchquery '$Subject' –LogLevel Full –Targetmailbox "$LoggingMailbox" –Targetfolder "FolderName" –DeleteContent | export-csv C:\$filename.csv

    Best Regards.


    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com

    Lynn-Li
    TechNet Community Support

    • Marked as answer by rush2ramki Wednesday, March 9, 2016 11:37 AM
    Wednesday, March 9, 2016 7:04 AM
  • Excellent Lynn

    Thanks for your great work.!

     
    Wednesday, March 9, 2016 11:36 AM