如何删除 Outlook 所有文件夹中某个日期之前的所有电子邮件?

如何删除 Outlook 所有文件夹中某个日期之前的所有电子邮件?

我错误地将一些旧电子邮件导入 Outlook,现在一切都乱了套。我想删除 Outlook 文件夹中某个日期之前的所有电子邮件DD/MM/YYYY。我尝试了 Windows Outlook 和 Web 应用程序,但每次只能选择有限数量的搜索结果,考虑到我错误导入的电子邮件数量,手动完成这个过程非常费力。

我进行了一些搜索,看看是否可以使用 PowerShell 来自动化该过程。我尝试过这一页例如,认为search-mailbox 命令在我的 PowerShell 环境中不可用。我还查看了这一页尽管它带来的困惑比帮助更大。

附言值得注意的是,我在这台装有 Windows 10 21H1 操作系统、Outlook 365 和 Powershell 的机器上拥有管理员权限5.1.*

答案1

您可以使用连接-ExchangeOnline

  1. 安装 Exchange 在线管理:
    Install-Module ExchangeOnlineManagement
    

  2. 跑步Search-Mailbox获取需要删除的电子邮件的日期,方法是:
    ### Recieved:
    -SearchQuery {Received:"<start date>..<end date>"}
    
    ### Sent:
    -SearchQuery {Sent:"<start date>..<end date>"}
    
    警告: 首先运行脚本## 2.部分以确保获得正确的结果并查看项目计数
    • PowerShell Exchange Online:

      ## 1. -- Connect to Exchange Online
      $emailAddr = "[email protected]";
      Import-Module ExchangeOnlineManagement;
      Try { Disconnect-ExchangeOnline; } Catch { $false };
      $ExoStatus = Try { Get-MailboxLocation $emailAddr; } Catch { $false; };
      If (!$ExoStatus){ Connect-ExchangeOnline -UserPrincipalName $emailAddr; };
      
      ## 2. -- This will give you an estimated count of items
      Search-Mailbox YourUsername -SearchQuery {Sent:"1/1/2000..12/31/2019"} -EstimateResultOnly;
      
      ## 3. -- This will actually delete the content for the date period specified
      Search-Mailbox YourUsername -SearchQuery {Sent:"1/1/2000..12/31/2019"} -DeleteContent;
      
    • PowerShell Exchange 本地部署:

      ### Run this to get a list of Exchange on-prem server names
      $e = (Get-ADDomain).DistinguishedName;
      $d = dsquery * "cn=Configuration,$e" -Filter "(objectCategory=msExchExchangeServer)";
      $d | % { $_.split(",")[0].Split("=")[1] };
      
      ## 1. -- Use this to connect to the Exchange on-prem server remotely
      $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://MyExchServerName.MyDomain.com/PowerShell/ -Authentication Kerberos
      Import-PSSession $Session -AllowClobber;
      
      ## 2. -- This will give you an estimated count of items
      Search-Mailbox YourUsername -SearchQuery {Received:"1/1/2000..12/31/2019"} -EstimateResultOnly;
      
      ## 3. -- This will actually delete the content for the date period specified
      Search-Mailbox YourUsername -SearchQuery {Received:"1/1/2000..12/31/2019"} -DeleteContent;
      
      Remove-PSSession $Session;
      

答案2

这是可以做到的使用 Outlook 客户端或者通过 PowerShell:

  1. 连接到 Exchange Online
    Import-Module ExchangeOnlineManagement
    
    Connect-ExchangeOnline -UserPrincipalName [email protected]
    

  2. 搜索邮件:
    New-ComplianceSearch -Name "Delete Message" -ExchangeLocation [email protected] -ContentMatchQuery '(Received:05/01/2022 00:00..05/01/2022 23:59)
    
    Start-ComplianceSearch -Identity "Delete Message"
    
    New-ComplianceSearchAction -SearchName "Delete Message" -Purge -PurgeType SoftDelete -force
    

相关内容