Delete inbox emails sent to a specific address across all mailboxes Powershell

Welcome Forums General PowerShell Q&A Delete inbox emails sent to a specific address across all mailboxes Powershell

This topic contains 1 reply, has 2 voices, and was last updated by

5 years, 9 months ago.

  • Author
  • #9542

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    I'm trying to run the script below and it works fine.

    The issue is that I want to only search the inbox and subfolders of the inbox, and exclude the sent folder is it possible?

    Ultimately the intention is that I wish to delete all emails that users send to the entire workplace distribution list, that are over a month old. But not delete the original email that was sent.

    I am new to scripting, and I have tried to get this working using EWS FindItems however the problem I encountered there is that I could figure out how to search through subfolders of inbox.

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    $DT = (Get-Date).AddMonths(-1).ToString("MM/dd/yyyy")
    Get-Mailbox | Search-Mailbox -SearchQuery " AND received:<$DT" -TargetMailbox "My Mailbox" -TargetFolder "SearchTest"

  • #9555

    Topics: 0
    Replies: 3
    Points: 0
    Rank: Member

    Unfortunately you cannot do this with Search-Mailbox. To do this with EWS you could try the following. This will enumerate all folders in a mailbox excluding Sent items. It should then output the emails that match your query and move them to the Deleted Items folder.

    After setting up the EWS service connection

    #Enumerates all folders in the Mailbox
    $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,""
    $TargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
    $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(10000);
    $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
    $Response = $tfTargetFolder.FindFolders($fvFolderView) | where-object {$_.DisplayName -ne 'Sent Items' -and $_.FolderClass -eq 'IPF.Note'}

    $ResultSize = 1000
    $view = New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList $ResultSize
    $SearchQuery = "*" #Put you AQS query here i.e. Subject:Hello*

    $Response | ForEach-Object {

    #Define which properties we want to retrieve from each message
    $propertyset = New-Object Microsoft.Exchange.WebServices.Data.PropertySet ([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)
    $view.PropertySet = $propertyset

    #Use FindItems method for the specified folder, AQS query and number of messages
    $items = $service.FindItems($,$SearchQuery,$view)

    #Loop through each message returned by FindItems
    $items | ForEach-Object {

    $emailProps = New-Object Microsoft.Exchange.WebServices.Data.PropertySet ([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
    $emailProps.RequestedBodyType = "Text"
    $email = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($service, $_.Id, $emailProps)

    #Create a custom object that returns the desired message properties
    New-Object PSObject -Property @{
    Subject = $email.Subject
    From = $email.Sender.Name
    To = $email.DisplayTo
    Sent = $email.DateTimeSent
    Mailbox = $Mailbox
    IsRead = $email.IsRead




The topic ‘Delete inbox emails sent to a specific address across all mailboxes Powershell’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort