Determine if a Mailbox is On-Premises or in Office 365 with PowerShell
One of the companies that I support is currently in the process of migrating from an on-premises Exchange Server environment to Office 365. They're currently running in hybrid mode. While it seems like wanting to know what mailboxes still exist onsite versus which ones are in the cloud would be an all too common task, there doesn't seem to be an easy way to get that information with PowerShell. You would think that you'd be able to run a PowerShell command and it would return the results. Well, not unless I'm missing something. There's no such command, or at least not one that's straight forward.
Note: This blog article is written using Windows 10 version 1803 and Windows PowerShell version 5.1. Your mileage may vary with different operating systems and/or different versions of PowerShell.
Follow the first few steps in
this previous blog article
to install the MSOnline
PowerShell module and connect to your Office 365 subscription. The
Get-MsolUser cmdlet which is
part of the MSOnline
PowerShell module will be used in this blog article.
At first, it doesn't appear that Get-MsolUser
returns any usable results for this type of
information, but it does return a MSExchRecipientTypeDetails
property which is a numeric value
that can be translated into the mailbox's location. One thing that's disappointing about this cmdlet
is there are almost no filters for it. Want unlicensed users, no problem, use the
UnlicensedUsersOnly
parameter. Want the opposite condition, we'll you're out of luck with the
exception of retrieving all of the users and piping them to
Where-Object.
Not real efficient when you have thousands of users and the majority of them have yet to be
licensed. Luckily, for these users, a usage location hasn't been assigned and won't be until they're
ready to be licensed. All of them reside in the United States so filtering using the UsageLocation
parameter is possible. Others may not be so lucky.
1Get-MsolUser -UsageLocation US |
2Where-Object isLicensed -eq $true |
3Select-Object -Property DisplayName, UserPrincipalName, isLicensed,
4 @{label='MailboxLocation';expression={
5 switch ($_.MSExchRecipientTypeDetails) {
6 1 {'Onprem'; break}
7 2147483648 {'Office365'; break}
8 default {'Unknown'}
9 }
10 }}
The previous command is written so that any users with a usage location of "US" that have a license will be returned in the results. The filter can easily be changed to meet your own needs. For example, to show unlicensed users and all of those will definitely be on-premises.
Another option is to use the
Get-Recipient function that's
part of the Exchange online PowerShell cmdlets. More information about those cmdlets can be found in
this blog article.
Get-Recipient
seems to have better options for filtering, but at the expense of being less
accurate. It doesn't seem to show mailboxes that only exist in Office 365 (ones that weren't
migrated) and things like the Discovery mailbox would need to be filtered out. Mailboxes that have
been migrated have a RecipientType
of UserMailbox
and the ones that still exist on-premises have
a RecipientType
of MailboxUser
.
1Get-Recipient -RecipientType UserMailbox, MailUser -ResultSize 25
If you know of an easier way to accomplish this task, please post it as a comment to this blog article.
µ