I figured it was time to bite the bullet and start converting all of my scripts from using the MSOnline V1 module to the new and shiny AzureAD 2.0 module.  Since I was finding few to no posts on pulling licensed users using the new module out there on the interwebs, I figured I’d share my findings.  You can get the AzureAd module quite easily from the PowerShell gallery by using the command:

Once you have that, you are ready to rock and roll.  The following script will connect to multiple AzureAD tenants, snag all AzureAD users, determine if they have a license assigned to them, and if they do export their UPN to a custom PowerShell object with the property name of Email Address.  I only did this because this is for marketing, and that’s what they wanted (they didn’t want to have to edit the export file at all).  But that’s enough explanation, let’s get to the good stuff.

 

Loosely based off of some of the examples found in this blog post.