Home > Blockchain >  Find oldest email via PowerShell
Find oldest email via PowerShell

Time:02-08

I found multiple posts regarding this topic, but I don't think they are correct.
The following script works great if you want to find the earliest/oldest item (which for my luck, it is a contact).

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

I want to find the earliest.. first email someone received. So someone helped me creating the following:

# 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  

That previous script works great, if... the email is not on the Purge folder. For some reason, when the emails are moved to the Purge folder, the ContainerClass is also removed. I know there is an email prior to 2/8/2016, but it is on the -Archive \Recoverable Items\ Purge

CodePudding user response:

If all you're doing is trying to avoid contact matching, change your ContainerClass to:

$_.ContainerClass -ne 'IPF.Contact'

That way you won't exclude the Purges folder. Alternatively you could adjust it to:

-and (($_.ContainerClass -eq 'IPF.Note') -or ($_.FolderPath -match "Purges"))

edit: I can't make comments to reply to you yet sorry, but yes Purges has no ContainerClass. It'll still proceed just fine with the 1st option (because a $null containerclass is still not equal to 'IPF.Contact'). Did you need to include contacts in the search?

CodePudding user response:

Thank you for your post Mike,

I did what you suggested, but it looks like the Purge folder removed the ContainerClass.

OldestItemReceivedDate FolderPath ContainerClass
---------------------- ---------- --------------
12/13/2016 6:03:09 PM  /Purges

The email (IPF.NOTE) was received on March 01, 2017... The one we see on the results is a calendar item.

I wish I could look for MSG files, that might do the trick, but I dont know how to do that.

  •  Tags:  
  • Related