Powershell script to delete emails from mailbox older than an 7 month or specified dates

5,587

Try:

New-ComplianceSearch -Name "Remove older than 7 month messages" -ExchangeLocation [email protected] -ContentMatchQuery "(Received >= 01/12/2017 -and Received <= $((get-date).AddMonths(-7).ToString("MM/dd/yyy")))"
Share:
5,587

Related videos on Youtube

mausasu1234
Author by

mausasu1234

Updated on September 18, 2022

Comments

  • mausasu1234
    mausasu1234 over 1 year

    I have problem with converting my Search-Mailbox command to another one. If you all know in the future as Search-Mailbox is retired. So I need to use another cmdlet for this command.

    My working right now command with Search-Mailbox

    Search-Mailbox -Identity [email protected] -SearchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))" -deletecontet
    

    I read a lot of times https://docs.microsoft.com/en-us/exchange/policy-and-compliance/ediscovery/delete-messages?view=exchserver-2019#step-2-delete-the-message and try do step by step and get this code.

    New-ComplianceSearch -Name "Remove older than 7 month messages" -ExchangeLocation [email protected] -ContentMatchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))"
    
    Start-ComplianceSearch -Identity "Remove older than 7 month messages"
    
    New-ComplianceSearchAction -SearchName "Remove older than 7 month messages" -Purge -PurgeType SoftDelete
    

    But its not work for me. getting error

    Unable to execute the task. Reason: The search "Remove older than 8 month messages" is still running or it didn't
    
    return any results. Please wait until the search finishes or edit the query and run the search again.
    

    ADD my full PowerShell script

    Start-Transcript
    
    
    $smtpServer="smtp.office365.com" # Office 365 official smtp server 
    $from = "IT Support <[email protected]>" # email from  
    $logging = "Enabled" # Set to Disabled to Disable Logging 
    $testing = "Disabled" # Set to Disabled to Email Users 
    $testRecipient = "[email protected]"  
    $date = Get-Date -format ddMMyyyy 
    
    $Username = "[email protected]"
    $Password = "test-" | ConvertTo-SecureString -AsPlainText -Force
    $UserCredential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password
    
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri  https://eur04b.ps.compliance.protection.outlook.com/powershell-liveid?PSVersion=5.1.17763.1007 -Credential $UserCredential -Authentication Basic –AllowRedirection
    
     Import-PSSession $Session
    
    Get-ComplianceSearchAction
    New-ComplianceSearch -Name "Remove older than 7 month messages" -ExchangeLocation [email protected]  -ContentMatchQuery "(Received:01/12/2017..$((get-date).AddMonths(-7).ToString("MM/dd/yyy")))"
    Start-ComplianceSearch -Identity "Remove older than 7 month messages"
    New-ComplianceSearchAction -SearchName "Remove older than 7 month messages" -Purge -PurgeType SoftDelete 
    
    • dortegaoh
      dortegaoh almost 4 years
      Have you tried to wait until the search finishes or edit the query and run the search again?
    • mausasu1234
      mausasu1234 almost 4 years
      im using script, it allways finishing search and asking then Confirm Are you sure you want to perform this action? This operation will make message items meeting the criteria of the compliance search "Remove older than 7 month messages" completely inaccessible to users. There is no automatic method to undo the removal of these message items. then i press Y or A i will get what error
    • dortegaoh
      dortegaoh almost 4 years
      Did you run Get-ComplianceSearchAction to check for already running searches?
    • mausasu1234
      mausasu1234 almost 4 years
      @GeraldSchneider yes i try it. i will post my all powershell script for to make my question more clearly
    • joeqwerty
      joeqwerty almost 4 years
      Wouldn't it be simpler to use a retention policy to do this?
    • mausasu1234
      mausasu1234 almost 4 years
      @joeqwerty i try it with with RetentionPolicy, i add it and dont delete anything
    • joeqwerty
      joeqwerty almost 4 years
      It can take up to 7 days to be applied. If you want to force it, follow this: docs.microsoft.com/en-us/microsoft-365/compliance/…
    • mausasu1234
      mausasu1234 almost 4 years
      i try this one, not working
  • mausasu1234
    mausasu1234 almost 4 years
    thank you, now then i using first New-ComplianceSearch then Get-ComplianceSearchAction i dont getting error, but then i press Y or A my window close very fast, but dont delete anything of emails.
  • mausasu1234
    mausasu1234 almost 4 years
    @Iwan_Wang your email have been deleted?
  • Ivan_Wang
    Ivan_Wang almost 4 years
    @mausasu1234 It didn't delete my emails, however, I tried to modify the "-ContentMatchQuery" parameter with a subject keyword, it worked. Based on the test result, it seems that the value of this parameter in the above command is inproper.
  • mausasu1234
    mausasu1234 almost 4 years
    @Iwan_Wang maybe do you know how to change it ? it seems getting date -7 mouth, but why didn't work i really don't understand
  • Ivan_Wang
    Ivan_Wang almost 4 years
    @mausasu1234 You could try the following command in my new reply. Hope it's helpful to you.
  • mausasu1234
    mausasu1234 almost 4 years
    still dont remove anything
  • Ivan_Wang
    Ivan_Wang almost 4 years
    @mausasu1234 Which emails do you want to delete? Based on the command you post above, the date range is from 01/12/2017 to the current date(Minus 7 months).
  • Ivan_Wang
    Ivan_Wang over 3 years
    @mausasu1234 Hi, i'm here to confirm if the compliance search issue has been fixed, if the above replies are helpful to you, you could mark the best answer. Have a nice day:-)