Office365 find license script

This topic contains 7 replies, has 3 voices, and was last updated by Profile photo of Iain Alexander Iain Alexander 1 year, 9 months ago.

  • Author
    Posts
  • #22802
    Profile photo of Iain Alexander
    Iain Alexander
    Participant

    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?

  • #22803
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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 [b]on the screen[/b]. 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.

  • #22804
    Profile photo of Iain Alexander
    Iain Alexander
    Participant

    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

  • #22806
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #22807
    Profile photo of Iain Alexander
    Iain Alexander
    Participant

    Ok thanks

  • #22814
    Profile photo of William Parry
    William Parry
    Participant

    Try this:

    Get-MsolUser -MaxResults 1 | select -expandproperty licenses

    I think that'll show what "sub property" of licenses it is you want.

  • #22815
    Profile photo of William Parry
    William Parry
    Participant

    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.

  • #22818
    Profile photo of Iain Alexander
    Iain Alexander
    Participant

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

    Your help is much appreciated everyone

You must be logged in to reply to this topic.