Office365 find license script

Welcome Forums General PowerShell Q&A Office365 find license script

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
3 years, 8 months ago.

  • Author
    Posts
  • #22802

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 7
    Rank: Member

    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.

  • #22804

    Participant
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 7
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Ok thanks

  • #22814

    Participant
    Points: 0
    Rank: Member

    Try this:

    Get-MsolUser -MaxResults 1 | select -expandproperty licenses

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

  • #22815

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

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

    Your help is much appreciated everyone

The topic ‘Office365 find license script’ is closed to new replies.