Hi @Avinash ,
Welcome to the Microsoft Q&A platform!
To achieve your goal, you can follow the steps below to connect to the Exchange Online module using global administrator credentials, loop through the list of users, and access each user's sent mailbox. Here is a sample PowerShell script to get you started:
- Install the Exchange Online PowerShell module if you haven't already:
Install-Module -Name ExchangeOnlineManagement -Force
- Connect to Exchange Online using global administrator credentials:
$UserCredential = Get-Credential
Connect-ExchangeOnline -Credential $UserCredential
- Define the list of users:
$users = @("******@domain.com", "******@domain.com", "******@domain.com")
- Loop through each user and retrieve the sent email ID:
foreach ($user in $users) {
# Grant full access to the global admin for the user's mailbox
Add-MailboxPermission -Identity $user -User $UserCredential.UserName -AccessRights FullAccess -InheritanceType All
# Access the user's sent mailbox
$mailbox = Get-Mailbox -Identity $user
$sentItems = Get-MailboxFolderStatistics -Identity $user | Where-Object {$_.FolderType -eq "SentItems"}
# Retrieve sent email IDs from the "To" and "CC" sections
$sentEmails = Get-MessageTrace -SenderAddress $user -StartDate (Get-Date).AddYears(-10) -EndDate (Get-Date)
$sentEmails | ForEach-Object {
$toRecipients = $_.Recipients | Where-Object {$_.RecipientType -eq "To"}
$ccRecipients = $_.Recipients | Where-Object {$_.RecipientType -eq "Cc"}
$toRecipients.EmailAddress, $ccRecipients.EmailAddress
}
# Remove full access permission after retrieving the data
Remove-MailboxPermission -Identity $user -User $UserCredential.UserName -AccessRights FullAccess -InheritanceType All
}
Make sure to adjust the script to your specific requirements and test it in a secure environment before running it in production.
Please feel free to contact me for any updates. And if this helps, don't forget to mark it as an answer.
Best,
Jake Zhang