Finding All Client Mailboxes in the Office 365 Partner Portal

Someone at work asked me “how many client mailboxes are we supporting in Office 365?”. It’s easy to tell how many clients you are supporting, as soon as you login to the partner portal it tells you that number. I had never tried to get the number of client mailboxes before, but I strongly suspected that I could do it using PowerShell. Turns out I was right!

The first thing you will need to do is to connect to Office 365 using PowerShell. If you haven’t done this before, go to this page and download the Office 365 PowerShell Module and install it.

After that is done, execute the commands below which will connect you to Office 365. It’s important to note that the credential you use must have delegated admin access to all the client accounts or the steps in this article won’t work.

Once that is done running the commands below will provide a list of all the commands in the MSOnline Module as well as only those commands which contain the word partner. I was hoping there was a cmdlet that would allow me to get all my clients information which I could then iterate through to get a list of all mailboxes.


The Get-MSOLPartnerContract looks interesting, lets try that.


Well, that gets me a lot of Tenant ID’s, which doesn’t appear to be useful. Or does it? I know from using Office 365 with PowerShell in the past that there are commands that use the TenantID parameter to retrieve information.

Doing some more investigation I run the series of commands below. First I am storing all the Tenant ID’s in a $Clients variable, and then using Get-Member to see what properties this variable contains. Seeing that one of them is Default Domain Name, I run $Clients.DefaultDomainName which sure enough enumerates all of my client domain names so I can see that they are in fact my clients. I am then storing all the Tenant ID’s in another variable $Tenants which I will be using shortly.

Earlier I mentioned that I knew from past experience that there were cmdlets in the MSOnline module that used TenantID as a parameter. How could I find out what those commands are? By running the command below. I reduced the output by using -Verb Get since I am going to be getting the number of mailboxes.

Scrolling through the list I see that Get-MSOLUser is one of the cmdlets that uses TenantID as a parameter and think that might be a good place to start. I take one of my Tenant ID’s in my $Tenants variable and run the command below.

I was hoping to see that there was a domain or client name property, but unfortunately there is not. So in order to figure that out I ran the commands below. What I am doing here is taking the first user that comes up for the tenant and getting their User Principal Name (UPN). Since each client’s users have the same UPN, this will be OK. After that I am splitting the UPN at the ‘@’ symbol and using that information for the domain. This is definitely not the only way to do this (or probably the best) but it worked for me.

Next I am creating a hashtable with my properties and then creating a new PSObject Object and saving this information to a .CSV file. Notice in the properties section that for the Domain I am having to use $Domain[1] because when it split the UPN at the ‘@’ symbol it split it into two pieces, the section before the ‘@’ and the section after it.

Finally, I have tied all this into a Function I am calling Get-PartnerMailboxes that will iterate through all my Tenant ID’s and create a .CSV listing the domain and total number of licensed users for each tenant.

6 thoughts on “Finding All Client Mailboxes in the Office 365 Partner Portal

  1. Your completed function is not an all-in-one function. It calls for the $tenants variable without defining it. Here is your function edited to work stand-alone 😉 Otherwise, GREAT work, this was extremely helpful!

    Function Get-PartnerMailboxes{
    [PSCredential]$Credential = (Get-Credential),

    Import-Module MSOnline
    Connect-MsolService -Credential $Credential

    $clients = (Get-MSOLPartnerContract)
    $tenants = $clients.tenantid

    ForEach($Tenant in $Tenants)
    $Users = Get-MsolUser -TenantId $Tenant| Where-Object {$_.IsLicensed -eq $True}
    $UPN = $Users[0].UserPrincipalName
    $Domain = $UPN.Split(‘@’)
    $Properties = @{‘Licensed Users’ = $Users.Count;
    ‘Domain’ = $Domain[1];}

    $PropsObject = New-Object -TypeName PSObject -Property $Properties
    $PropsObject | Export-CSV -Path $Path -NoTypeInformation -Append

    Get-PartnerMailboxes -Path “C:\your path\partnermailboxes.csv”

  2. Hi Jacob,

    I noticed you are using the older MSOnline PowerShell module in your examples. It may be useful to start using the newer Azure Active Directory PowerShell V2 module instead, as we will begin deprecating the MSOnline module when we have migrated the functionality of the MSOnline module to the newer module – currently planned for the Spring of 2017.


    Rob de Jong

    • Rob –

      Way late to this but thanks for the heads up. I didn’t know that was coming or available. I am going to update this post and another post I have coming on finding unlicensed users to use the new module.

  3. Great article. I get a blank spreadsheet thought. I’ve stepped through various steps and seem to be getting data so I know its not dead

Leave a Reply