PowerShell Commands to Extract External User Email Addresses

Mohana Reddy 100 Reputation points
2025-01-30T19:11:59.9333333+00:00

There is a need to retrieve email address information of external users for supporting an email campaign. Looking for PowerShell commands that can extract all email addresses from the mailbox, specifically targeting external users outside the organization.

Additionally, the goal is to perform this action for all users in the organization simultaneously.

Note: Emails from the past 4-5 years are visible in the Outlook account, but verifying every email manually is a big process. Any assistance or possibilities to retrieve this data would be appreciated.

Microsoft Exchange Online
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
5,462 questions
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,734 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,795 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Zhang-MSFT 4,580 Reputation points Microsoft Vendor
    2025-01-31T07:03:11.8366667+00:00

    Hello, @Mohana Reddy,

    Welcome to the Microsoft Q&A platform!

    Here's a general approach to extract email addresses of external users from mailboxes:

    1.Connect to Exchange Online: First, ensure you have the Exchange Online PowerShell module installed. Then, connect to Exchange Online:

    Connect-ExchangeOnline -UserPrincipalName ******@domain.com -ShowProgress $true
    

    2.Retrieve External Email Addresses: You can use the following script to extract email addresses from all mailboxes. This script filters out internal email addresses based on your domain:

    $domain = "yourdomain.com"
    $externalEmails = @()
    
    $mailboxes = Get-Mailbox -ResultSize Unlimited
    foreach ($mailbox in $mailboxes) {
        $emails = Get-MailboxFolderStatistics -Identity $mailbox.UserPrincipalName | Where-Object {$_.FolderType -eq "Inbox"} | ForEach-Object {
            Get-Content -Path $_.FolderPath
        }
        foreach ($email in $emails) {
            if ($email.SenderEmailAddress -notlike "*@$domain") {
                $externalEmails += $email.SenderEmailAddress
            }
        }
    }
    
    $externalEmails | Sort-Object -Unique | Out-File "ExternalEmailAddresses.txt"
    

    (Please Note: Replace "yourdomain.com" with your actual domain. This script assumes that external email addresses do not contain your domain name and saves the unique external email addresses to a file named ExternalEmailAddresses.txt.)

    Should you need more help on this, you can feel free to post back. 


    If the answer is helpful, please click on “Accept answer” as it could help other members of the Microsoft Q&A community who have similar questions and are looking for solutions.

    Thank you for your support and understanding.

    Best Wishes,

    Alex Zhang

    0 comments No comments

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.