Finding All Tenants Unassigned Office 365 Licenses

I previously wrote about finding all the mailboxes across your Office 365 tenants, and today I am going to share a function I wrote to find all the unassigned Office 365 licenses across those tenants.  First, I should note that this function is modified off of one I found on the TechNet script gallery but for some reason even after searching through it I cannot seem to find it.  I am also using the MSOnline module for this, even though the new AzureAD module has supposedly replaced it. I have been in contact with someone on the team at Microsoft about what the current limitations are.  However, the functionality doesn’t exist in that module (yet) to get the information that we need so I am continuing to use the MSOnline Module instead.

It’s a pretty straight forward function so I am just going to break down what it does:

  • Connects to Office 365.  You will need to use an account that has delegated admin permissions across your tenants
    • If you have MFA enabled you won’t be able to login (or at least I couldn’t get it to work with my account)
  • Gets a list of all the tenants you have delegated admin access for
  • Creates an Array to store all the information
  • Gets all the associated AccountSkus from each tenant, with the exception of Power_BI and PowerApps
  • Compares the value of ConsumedUnits to ActiveUnits.  If the amount of ConsumedUnits is less than the amount of ActiveUnits, we know that there are unassigned licenses.

That’s all there is to it.  Full function is below.