Variable (Array) in Get-MgUserLicenseDetail

Hi everyone,
Environment: MicrosoftGraph and PowerShell
Goal: List all users and display the services they have booked for them

To see the assigned services for a specific user (for example, “clara.korn@company.com”), I use:
Get-MgUserLicenseDetail -UserId "clara.korn@firma.de" | Select-Object SkuPartNumber

When I continued editing the script, I wrote the email address into a variable and adjusted my Get-MgUserLicenseDetail command line:
$mymail = "clara.korn@firma.de"
Get-MgUserLicenseDetail -UserId $mymail | Select-Object SkuPartNumber

Now, however, I have several email addresses that I want to read. I do this by reading all users and retrieving the userPrincipalName. Then, for verification, I output the email address at array position 117. This is the email address of “clara.korn@firma.de”:
$pn = Get-MgUser -All:$True | Select-Object userPrincipalName
echo $pn[117]

No problems here either. The email address is displayed. However, as soon as I want to access the associated services, the variable (array) is not accepted:
Get-MgUserLicenseDetail -UserId $pn[117] | Select-Object SkuPartNumber

I get the following error:
Get-MgUserLicenseDetail : Resource '@{UserPrincipalName=clara.korn@firma.de}' does not exist or one of its queried reference-property objects is not present.

It looks to me as if the entry UserPrincipalName=clara.korn@firma.de is at array position 117. But I only need the email address, without the preceding UserPrincipalName=

How can I accomplish this?

Thanks and happy coding Dirkules

You can either use ExpandProperty to get just the userPrincipalName values:

$pn = Get-MgUser -All:$True | Select-Object -ExpandProperty userPrincipalName

Or use dot notation to access the property value:

Get-MgUserLicenseDetail -UserId $pn[117].userPrincipalName | Select-Object SkuPartNumber

Thank you very much. It is working now but it was necessary to edit the $pn line to:
$pn = Get-MgUser -All:$True
or
$pn = Get-MgUser

Another key was your hint with
-UserId $pn[117].userPrincipalName

Dirkules

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.