Home > Software engineering >  PowerShell - Find Oldest Email
PowerShell - Find Oldest Email

Time:12-03

I am stuck, I am trying to find the oldest "EMAIL" in a person's mailbox, but I don't know what else to try. I think I need to add the ContainerClass -eq "IPF.Note" somewhere, but I am not sure where.

The following script works, but it finds the oldest ITEM, which in my case it is a contact. I want to look at each container (Email, Chats, Calendar, Contacts) separately, but for this script, I just want to know the oldest email.

Thank you

Get-MailboxFolderStatistics -IncludeOldestAndNewestItems -Identity USERID | 
    Where OldestItemReceivedDate -ne $null | 
    Sort OldestItemReceivedDate | 
    Select -First 1 OldestItemReceivedDate

CodePudding user response:

Presuming this is for compliance reasons to search a mailbox for items on an Exchange Server you should be using the Search-Mailbox cmdlet - https://docs.microsoft.com/en-us/powershell/module/exchange/search-mailbox?view=exchange-ps

For Exchange Online to search a mailbox for items you should use the New-ComplianceSearch cmdlet https://docs.microsoft.com/en-us/powershell/module/exchange/new-compliancesearch?view=exchange-ps

This web page shows how to search by date - New-ComplianceSearch: how to use the newer version of Search-Mailbox https://www.codetwo.com/admins-blog/new-compliancesearch-new-version-of-search-mailbox/

This web page has a script to search mailboxes, including dates PowerShell – New-ComplianceSearch script to go through all mailboxes, find a target message, and remove it - https://365basics.com/powershell-new-compliancesearch-script-to-go-through-all-mailboxes-find-a-target-message-and-remove-it/

Using your original approach, should be done like this. Presuming you have appropriate permissions.

Get-MailboxFolderStatistics -ID <mailboxemailaddress> -IncludeOldestAndNewestItems | select Identity, Name, FolderPath, ItemsInFolder, FolderSize, OldestItemReceivedDate | Export-Csv C:\temp\Mailbox.csv -NoTypeInformation

CodePudding user response:

You can filter what you have by item type, but I would do it after getting the statistics so you only have to query exchange once:

# Get the folder statistics for all folders
$stats = Get-MailboxFolderStatistics -IncludeOldestAndNewestItems -Identity $USERID

# Get the oldest email. Can re-use $stats for the other item types
$OldestEmail = $stats |
  Where-Object {$_.OldestItemReceivedDate -and $_.ContainerClass -eq 'IPF.Note'} |
  Sort-Object OldestItemReceivedDate |
  Select-Object ContainerClass,OldestItemReceivedDate,FolderPath -First 1

# Outputs
ContainerClass OldestItemReceivedDate FolderPath
-------------- ---------------------- ----------
IPF.Note       2/8/2016 2:07:50 PM    /Inbox    

You are correct that the mailbox folder statistics command does not search recoverable items by default. It also does not search the mailbox archive unless you specify -Archive. If you need these, you'll have to do additional searches:

# Get recoverable items:
Get-MailboxFolderStatistics -Identity $USERID -FolderScope 'RecoverableItems' -IncludeOldestAndNewestItems |
  Where-Object OldestItemReceivedDate |
  Sort-Object  OldestItemReceivedDate |
  Select-Object ContainerClass,OldestItemReceivedDate,FolderPath -First 1

# Note that deleted item containers do not have an item type!
ContainerClass OldestItemReceivedDate FolderPath       
-------------- ---------------------- ----------       
               2/5/2016 3:41:33 PM    /Deletions       
  • Related