Creating license reports with Powershell and AAD


I’m working on creating reports for assigned Microsoft licenses in our AAD. Basically what we need is to grab DisplayName, UserPrincipalName, Company and whatever licenses are assigned to the user.

I found this script: Export Office 365 User License Report With PowerShell that I’ve modified a bit to get proper encoding, delimiters and other more cosmetic changes.

A problem is that we use the Company property in our AD/AAD for billing purposes, but that property seems to not be exposed via the Get-MsolUser cmdlet. The Get-AzureADUser cmdlet seems to expose this property, but Get-AzureADUser does not support the same switches for license management as Get-MsolUser.

Is there a reasonable way to get licensing information with the use of the AzureAD (preview) module, or do I have to go via the MsOlService module, and if so can I combine the two modules in a clever way to get the properties I need.

Pseudo-code, but it’s data from two places with multiple unique identifiers (samAccountName, email, DN), so you would just get user information from both sources:

$uniqueValue = 'samAccountName'
$msUser = Get-MsolUser $uniqueValue
$azUser = Get-AzureADUser $uniqueValue

    Name  = $msUser.DisplayName
    Email = $msUser.Email
    Company = $azUser.Company


$msolUser | Select Name,
                   @{Name='Company';Expression={Get-AzureAdUser -Filter {Mail -eq $_.Email} | Select -ExpandProperty Company}

I looked at the technet gallery script that you are using. You can replace the following code snippets:

# Add this line to the top of Get_UsersLicenseInfo function

$company = $_.CompanyName

# Old Code to Replace

Get-MsolUser -UserPrincipalName $item.displayname | where { $_.islicensed -eq "true" } | Foreach {

# New Code For Above Snippet

$companyName = Get-AzureADUser -SearchString $item.displayname | select -Expand CompanyName
Get-MsolUser -UserPrincipalName $item.displayname | where { $_.islicensed -eq "true" } | select *,@{ n = "CompanyName"; e= { $companyName } } | Foreach {

# Old Code to replace

$Result = @{'DisplayName'=$_.Displayname;'UserPrinciPalName'=$upn;'LicensePlan'=$Licenseitem;'FriendlyNameofLicensePlan'=$nameprint;'ServiceName'=$service.ServicePlan.ServiceName;'FriendlyNameofServiceName'=$serviceFriendlyName;'ProvisioningStatus'=$service.ProvisioningStatus}

# New Code for above snippet

$Result = @{'DisplayName'=$_.Displayname;'UserPrinciPalName'=$upn;'LicensePlan'=$Licenseitem;'FriendlyNameofLicensePlan'=$nameprint;'ServiceName'=$service.ServicePlan.ServiceName;'FriendlyNameofServiceName'=$serviceFriendlyName;'ProvisioningStatus'=$service.ProvisioningStatus;'Company'=$company}


Sorry for the late reply.

I want to thank you both for your input, it helped me get to the right place.

In the end I was asked to get even more info which was not exposed by either MSOnlineService or AzureAD, so I changed the script to grab that info directly from the local AD instead.