Office365 find license script

Hi

I have created a script that connects to our Office365 tenancy and finds the license assigned to each user (amongst other things)

Get-MsolUser -All | where {$_.IsLicensed -eq "true" } | 
Select-Object -Property DisplayName, Licenses, MobilePhone, UserPrincipalName 

This works fine but shows the license in curly braces e.g. {“my tenancy”:STANDARDPACK} which when I pipe to export-csv then appears as System.Collections.Generic.List`1[Microsoft.Online.Administration.UserLicense]

Anyone know how I can resolve that so it shows the same as when it is output to the screen please?

That’s because Licenses is not a property; it is a collection that can contain multiple values. The curly bracket thing is what PowerShell does when you ask it to display a collection as a string on the screen. When you export, the curly bracket thing doesn’t come into play, and so you get the type name of the collection.

Select-Object -Property DisplayName, @{n='Licenses';e={ ($_ | Select -Expand Licenses) -join "/" }, MobilePhone, UserPrincipalName

Will output a slash-delimited list of licenses. You can fiddle with that to get the look you want. I’m just guessing on what you might want to see, so this will probably require some adjusting.

Hmm - when I ran

Get-MsolUser -MaxResults 1 | gm
it listed Licenses as a property.

Tried your suggestion and it now shows as Microsoft.Online.Administration.UserLicense both on screen and in csv

It’s a property that contains a collection. Unforunately, I can’t run the same command so I cannot troubleshoot that for you. The syntax I gave may not be exactly correct for your situation, but it’s the general direction. It seems like the property is a collection of other objects, which means additional selecting would need to be done. Let me see what I can research.

Ok thanks

Try this:

Get-MsolUser -MaxResults 1 | select -expandproperty licenses

I think that’ll show what “sub property” of licenses it is you want.

I think what you want, sorry if you would have found this yourself, is:

Get-MsolUser -All | where {$.IsLicensed -eq “true” } |
Select-Object -Property DisplayName, @{name=“licenses”;expression={$
.licenses.accountskuid}}, MobilePhone, UserPrincipalName

The property you saw before adding Export-CSV to the pipeline was actually the licenses.accountskuid property.

Ah that got it thanks William. I had tried {$_.accountskuid} and not got anywhere with it.

Your help is much appreciated everyone