Home > Net >  PowerShell - Using PSCustomObject to rename MsolUser property
PowerShell - Using PSCustomObject to rename MsolUser property

Time:01-06

I am trying to create a script that will import a csv of UPNs and get the 365 licenses assigned. To get the license we have to use {$.licenses.accountskuid}. When I export to file the header is of course {$.licenses.accountskuid}. I am using PSCustomObject to attempt cleaning up the final result but not sure what I am missing.

Here is the script. The exported file is showing the column header exactly how I want but there is no data under {$.licenses.accountskuid}. All the data shows properly for the other columns. I'm sure I just have the wrong syntax for {$.licenses.accountskuid}. I'm still learning PowerShell so if there is an easier way, I am all ears and willing to learn. Thanks

`Import-CSV -Path "C:\Temp\users.csv" | ForEach-Object {
$msUser = Get-MsolUser -UserPrincipalName $_.UserPrincipalName 
    [PScustomobject]@{
            UserPrincipalName = $msUser.UserPrincipalName
            DisplayName       = $msUser.FullName
            IsLicensed        = $msUser.IsLicensed
            LicenseSKU        = $msUser.{$_.licenses.accountskuid} }
            } |
Export-CSV -Path "C:\temp\output.csv" -Append -NoTypeInformation`

CodePudding user response:

Continuing from my comment and to answer your secondary question how to get the friendly name for an SKU product code:

# create a lookup Hashtable to convert the SKU product codes to friendly names.
# for demo, this is a shortened list. If you want the complete (MASSIVE) lookup table see
# https://scripting.up-in-the.cloud/licensing/list-of-o365-license-skuids-and-names.html
$skuids = @{
    'AAD_BASIC'='Azure Active Directory Basic'
    'INTUNE_A'='Microsoft Intune'
    'O365_BUSINESS'='Microsoft 365 Apps for Business'
    'OFFICE_BASIC'='Office 365 Basic'
    'SHAREPOINTWAC_DEVELOPER'='Office for the Web for Developer'
    'SHAREPOINTWAC_EDU'='Office for the Web for Education'
    'SPB'='Microsoft 365 Business Premium'
    'SPE_E3'='Microsoft 365 E3'
    'SPE_E5'='Microsoft 365 E5'
    'SPE_F1'='Microsoft 365 F3'
    'STANDARDPACK'='Office 365 E1'
    'TEAMS1'='Microsoft Teams'
    'VISIO_CLIENT_SUBSCRIPTION'='Visio Online'
    'WIN10_PRO_ENT_SUB'='Windows 10/11 Enterprise (Original)'
    'YAMMER_ENTERPRISE'='Yammer Enterprise'
}


Import-CSV -Path "C:\Temp\users.csv" | ForEach-Object {
    $msUser  = Get-MsolUser -UserPrincipalName $_.UserPrincipalName 
    # get the SKU product id part of the user license and try to
    # convert that to its friendly name using the $skuids Hashtable
    $license = ($msUser.licenses.AccountSkuId -split ':')[-1].Trim()
    if ($skuids.ContainsKey($license)) { $license = $skuids[$license] }

    [PScustomobject]@{
        UserPrincipalName = $msUser.UserPrincipalName
        DisplayName       = $msUser.FullName
        IsLicensed        = $msUser.IsLicensed
        LicenseSKU        = $license
    }
} | Export-CSV -Path "C:\temp\output.csv" -Append -NoTypeInformation
  • Related