How to get all users with alias as subdomain

Mani 376 Reputation points
2025-01-30T20:46:33.41+00:00

Hello All,

We need to pull a report where users having alias as our subdomains

We have around 20k users and we need to filter out them with our subdomains (15).

Please suggest a quick and easy to figure it out, pulling out the entire report and checking is consuming lot of time.

Example: Mani is having primary smtp with microsoft and alias with onmicrosoft, contoso

And i need to pull report for contoso from all 20k users.

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,781 questions
Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
2,248 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,852 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alex Zhang-MSFT 6,005 Reputation points Microsoft External Staff
    2025-01-31T05:20:41.2466667+00:00

    Hello, @Mani,

    Welcome to the Microsoft Q&A platform!

    To efficiently pull a report of users with specific subdomain aliases, you can use PowerShell. Here's a script that can help you filter out users with the "contoso" subdomain from your 20k users:

    1.Open PowerShell with administrative privileges.

    2.Run the following script:

    # Connect to Exchange Online
    Connect-ExchangeOnline -UserPrincipalName ******@domain.com -ShowProgress $true
    
    # Define the subdomain to filter
    $subdomain = "contoso"
    
    # Get all users and filter by alias
    $users = Get-Mailbox -ResultSize Unlimited | Where-Object {
        $_.EmailAddresses -match $subdomain
    }
    
    # Export the filtered users to a CSV file
    $users | Select-Object DisplayName, PrimarySmtpAddress, EmailAddresses | Export-Csv -Path "C:\Users\YourUsername\Documents\FilteredUsers.csv" -NoTypeInformation
    
    # Disconnect from Exchange Online
    Disconnect-ExchangeOnline -Confirm:$false
    

    Test Results: User's image

    This script should help you quickly generate the report you need without manually checking each user. Let me know if you need any further assistance!


    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


1 additional answer

Sort by: Most helpful
  1. Vasil Michev 114.9K Reputation points MVP
    2025-01-31T08:35:18.0966667+00:00

    You can use server-side filtering for that. For example, to list all recipients associated with the .onmicrosoft.com domain, you can do this:

    Get-Recipient -RecipientPreviewFilter {EmailAddresses -like "*@tenant.onmicrosoft.com"}
    

    If you only care about mailboxes, use Get-Mailbox with the -Filter parameter instead.


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.