How to map Mailbox object with AzureAD user object using Powershell

Recently I wrote a Powershell script to find disabled users that are associated with a particular set of mailboxes, for this need, I have to first get mailboxes using the Exchange Online Powershell cmdlet Get-Mailbox, then I need to find Azure AD object for the required mailbox using Get-AzureADUser cmdlet. The Get-AzureADUser cmdlet accepts the ID parameter only as a UPN or ObjectId of a user in Azure AD. After exploring Mailbox object attributes, I don’t find any attribute with the name UserPrincipalName or ObjectId and the properties Id, Guid and Identity are not suitable here as they hold different values and finally found the attribute ExternalDirectoryObjectId perfectly holds same value as its equivalent Azure AD object’s ObjectId value.

Note: You might have noticed the properties WindowsEmailAddress and PrimarySmtpAddress got the value as same UserPrincipalName in Azure AD, but I have not preferred any of these two fields as it may or may not be equivalent with UPN in all cases.

$mailbox = Get-Mailbox -Identity "Alex Wilber"
$azureADuser = Get-AzureADUser -Object $mailbox.ExternalDirectoryObjectId

$mailbox.ExternalDirectoryObjectId
$azureADuser.ObjectId
How to find AzureAD user object for its equivalent mailbox object in Powershell

You can also find the attribute ExternalDirectoryObjectId with other Exchange powershell cmdlets like Get-Recipient.

$mailbox = Get-Recipient -Identity "Alex Wilber"
$azureADuser = Get-AzureADUser -Object $mailbox.ExternalDirectoryObjectId

Advertisement

Leave a Comment