Mailbox Name Search
Review Exchange Online mailbox-search examples matching addresses, user principal names and display names. The notes include pattern examples and enabled/disabled mailbox filtering.
Source-derived reference
These notes were recovered from OneNote. Configuration, permissions and operating results have not been freshly verified. Confirm the applicable authority and environment before acting.
Search the emailAddresses, UserPrincipalName and DisplayName attributes of all Mailboxes in ExchangeOnline for
a match on a search string or pattern.
Search for mailbox in Exchange using a string "jones" or pattern "^hr.@chin." and comparing it to the Mailbox attributes
emailAddresses(proxyAddresses), UserPrincipalName and DisplayName attributes.
By default both disable and enabled mailboxes are searched. See "FILTER OUT DISABLED MAILBOXES" if you need to filter out disabled mailboxes.
EXAMPLE 1
#Search for all Mailboxes that contain the string "Jones"
$SearchString = "Jones"
EXAMPLE 2
#Search for Mailboxes that start with hr.
#^ means start of line
#.* means 0, 1 or more of any character
#"^hr.*"
$SearchString = "^hr.*"
EXAMPLE 3
#Search for Mailboxes that start with Primary.
#^ means start of line
#.* means 0, 1 or more of any character
#"^hr.*"
$SearchString = "^Primary.*"
EXAMPLE 4
#Search for all Mailboxes in Western Australia
#$ in a pattern means "end of line"
#.* means 0, 1 or more of any character
SearchString = ".*@.*wa.edu.au"
#Connect to ExchangeOnline
Connect-ExchangeOnline
#Set Search String From Example 2:
$SearchString = "^Primary.*"
RETURNS ABOUT 15 MAILBOX ATTRIBUTES #Returns All Mailbox Attributes
$exchangeResult = Get-ExoMailbox -ResultSize Unlimited | Where-Object {$.emailaddresses -match "$SearchString" -or $.UserPrincipalName -match "$SearchString" -or $.DisplayName -match $SearchString} | Select-Object *,@{n="proxyAddressMatches";e={foreach($email in $.EmailAddresses){if($email -match $SearchString){$email}}}}
RETURNS 3 MAILBOX ATTRIBUTES
#DisplayName, UserPrincipalName and additional custom attribute called proxyAddressMatches. #proxyAddressMatches is taken from emailAddresses Mailbox attribute which it known as proxyAddresses in EntraId.
$exchangeResult = Get-ExoMailbox -ResultSize Unlimited | Where-Object {$.emailaddresses -match "$SearchString" -or $.UserPrincipalName -match "$SearchString" -or $.DisplayName -match $SearchString} | Select-Object DisplayName, UserPrincipalName, @{n="proxyAddressMatches";e={foreach($email in $.EmailAddresses){if($email -match $SearchString -and $email -notmatch "X500"){$email}}}}
FILTER OUT DISABLED MAILBOXES
#Add in the following filter to either of the above commands.
#Read more about this filter at https://learn.microsoft.com/en-us/powershell/exchange/filter-properties?view=exchange-ps#ismailboxenabled
$exchangeResult = Get-ExoMailbox -Filter 'IsMailboxEnabled -eq $true' -ResultSize Unlimited | Where-Object…
DISPLAY RESULT #Show the first result $exchangeResult | Select-Object -First 1
JT Ref: Above is taken from Search-XforEmailAddress (CCMX.Exch.psm1)