Report Domains used for Mailboxes in ExchangeOnline
 
		
	Hi All,
Recently i had a Customer that wantet to figure out what Domains are used by his Exchange Online Mailboxes.
First we need to figure out, what accepted Domains he owns. That can be archieved, by the following command
Connect-ExchangeOnline -ShowBanner:$false
Get-AcceptedDomain
The Emailadresses are stored in the Attributes EmailAddresses (Array) and PrimarySMTPAddress
 Get-Mailbox -Identity m.muster@icewolf.ch | fl EmailAddresses, PrimarySMTPAddress
Let’s get all Mailboxes and use a Where-Object Filter
Mailboxes = Get-Mailbox -ResultSize Unlimited
[Array]$RecipientAddress = $Mailboxes| where-object {$_.EmailAddresses -like "*@icewolf.ch"}
$RecipientAddress.Count
$RecipientAddress
I’ve writen a small Script to Report the Emailaddresses and PrimarySMTPAddress for each Domain
The Script can be found also on my GitHub Repo
###############################################################################
# Report EmailAddresses and PrimaryEmailaddress per AcceptedDomain
# 2023.01.25 - V1.0 - Andres Bohren
###############################################################################
#Connect-ExchangeOnline
If ($Null -eq $(Get-ConnectionInformation))
{
	Write-Host "Connect-ExchangeOnline" -ForegroundColor Green
	Connect-ExchangeOnline -ShowBanner:$false
}
#Get-AcceptedDomain
Write-Host "Getting AcceptedDomains..." -ForegroundColor Green
$AcceptedDomains = Get-AcceptedDomain
Write-Host "Getting Mailboxes..." -ForegroundColor Green
$Mailboxes = Get-Mailbox -ResultSize Unlimited 
#Loop through AcceptedDomains
$Results = @() 
$INT = 0 
Foreach ($AcceptedDomain in $AcceptedDomains) 
{ 
	$INT = $INT + 1 
	$Domain = $AcceptedDomain.DomainName 
	Write-Host "Working on Domain: $Domain [$INT]" -ForegroundColor Green 
	#Additional EmailAddresses
	[Array]$RecipientAddress = $Mailboxes| where {$_.EmailAddresses -like "*@$Domain"}
	$RecipientCount = $RecipientAddress.Count 
	Write-Host "EmailAddressesCount: $RecipientCount" 
	#PrimaryEmailaddress
	[Array]$PrimaryRecipients = $Mailboxes| where {$_.PrimarySMTPAddress -like "*@$Domain"}
	$PrimaryRecipientCount = $PrimaryRecipients.Count 
	Write-Host "PrimaryAddressCount: $PrimaryRecipientCount" 
	#Create PSCustomObject
	$myObject = [PSCustomObject]@{ 
	Domain     = $domain 
	EmailAddresses = $RecipientCount 
	PrimaryEmailaddress  = $PrimaryRecipientCount 
	} 
	#Add to Results Array
	$Results += $myObject
}
$Results
$CSVPath = "$PSScriptRoot\EmailAddressesPerDomain.csv"
Write-Host "Exported to $CSVPath"
$Results | Export-Csv -Path "$PSScriptRoot\EmailAddressesPerDomain.csv" -NoTypeInformation
The Output is stored as a CSV File in the Script Directory
Regards
Andres Bohren
 
				





