PowerShell Script for Accessing Sent Emails in Outlook and Exchange

Avinash 20 Reputation points
2025-02-02T05:13:01.9+00:00

I am developing a PowerShell script to retrieve sent email IDs from my Outlook sent mailbox for both the "To" and "CC" sections, from the beginning of the email account to the current date. I managed to create a script that works for a single user; however, it prompts for a notification acceptance when running in PowerShell.

The updated requirement is to extend this functionality to multiple users. The script should connect to the Exchange module with global admin credentials, access the Exchange Admin Center, and then loop through a list of users specified in the script. The goal is to access each user's sent mailbox to copy the sent email IDs and generate the output.

I am encountering errors while attempting to connect and access users in this manner. Assistance is needed in finding the correct command to connect to this environment.

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,739 questions
Microsoft Exchange
Microsoft Exchange
Microsoft messaging and collaboration software.
672 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,625 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jake Zhang-MSFT 9,010 Reputation points Microsoft Vendor
    2025-02-03T05:54:37.13+00:00

    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:

    1. Install the Exchange Online PowerShell module if you haven't already:
    Install-Module -Name ExchangeOnlineManagement -Force
    
    1. Connect to Exchange Online using global administrator credentials:

    $UserCredential = Get-Credential

    Connect-ExchangeOnline -Credential $UserCredential
    
    1. Define the list of users:
    $users = @("******@domain.com", "******@domain.com", "******@domain.com")
    
    1. 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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.