Hey everyone! I'm trying to extract emails from multiple users' mailboxes using PowerShell, but I'm running into a problem with duplicate message IDs in the results. I'm using a filter based on a threshold date and predefined folders to exclude certain messages. Despite this, I still get duplicates. Here's a bit of my code for context:
```powershell
$targetFolderName = "Mail Retention cleanup"
$dateThreshold = Get-Date "07-03-2025"
$time = $dateThreshold.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.000Z")
$folder1 = Get-MgUserMailFolder -UserId $manUser -MailFolderId 'deleteditems'
$folder2 = Get-MgUserMailFolder -UserId $manUser -MailFolderId 'sentitems'
$folder3 = Get-MgUserMailFolder -UserId $manUser -Filter "DisplayName eq '$($targetFolderName)'"
$1mails = Get-MgUserMessage -UserId $manUser -Filter "ReceivedDateTime ge $time and not(ParentFolderId eq '$($folder1.Id)' or ParentFolderId eq '$($folder2.Id)' or ParentFolderId eq '$($folder3.Id)')" -All -PageSize 999 -Property ReceivedDateTime, Subject, ParentFolderId, InternetMessageHeaders
```
I've tried grouping the messages by ID to find duplicates, but the subjects are often different. Any insights on why this might be happening?
3 Answers
It sounds like the duplicate issue could be related to case sensitivity in message IDs. Have you tried checking if the IDs are being treated the same regardless of casing? Using `-CaseSensitive` in your filters might help!
I noticed some weird formatting in your code. Make sure you’re not mixing inline code with code blocks; that can really confuse things. Try copying your code into a PowerShell editor first to clarify the syntax, then paste it here. Here’s a tip: hitting Tab to indent can help the readability.
Also, check that your folder IDs are correct. If your debug process shows files being moved to folders you’re excluding, you might not be seeing them in your results. Running your code line by line can shed light on what's going on!
Yeah, I checked the IDs and they seem correct. Any time emails are moved to those folders, they’re excluded from my output, which is good!
I ran into the same issue! Once I adjusted for case sensitivity, it worked much better. Funny how easy it is to overlook something like that!