[Powershell] AzureAD LicencePlan SKU to variable

freumich 21 Reputation points

Hello Everyone

I try to get a output of a Powershell script into a variable but with no luck.

Here the script who is based on this article: https://learn.microsoft.com/en-us/microsoft-365/enterprise/view-account-license-and-service-details-with-microsoft-365-powershell?view=o365-worldwide

$licensePlanList = Get-AzureADSubscribedSku
$userList = Get-AzureADUser -ObjectID $userUPN | Select -ExpandProperty AssignedLicenses | Select SkuID
$userList | ForEach { $sku=$.SkuId ; $licensePlanList | ForEach { If ( $sku -eq $.ObjectId.substring($.ObjectId.length - 36, 36) ) { Write-Host $.SkuPartNumber } } }

Here is a example of the output: M365EDU_A5_STUDENT

I tried several things including Tee-Object but with no success. The variable keeps empty.
Can someone lead me in the right direction to get the output into a variable?

Thanks in advance!

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
17,476 questions
0 comments No comments
{count} votes

Accepted answer
  1. Shashi Shailaj 7,551 Reputation points Microsoft Employee

    @freumich ,
    Thank you for your query . I understand that you are trying to get details of AzureAD license Plan through the mentioned code snippet and store the output in a variable.

    I modified the above code snippet a little so that rather than posting the output to the console using Write-Host , it now stores that value as an array in the variable called $userSku .

    $userUPN = "abcdef@test21.onmicrosoft.com"  
    $licensePlanList = Get-AzureADSubscribedSku  
    $userList = Get-AzureADUser -ObjectID $userUPN | Select -ExpandProperty AssignedLicenses | Select SkuID   
    $userList | ForEach { $sku=$_.SkuId ; $licensePlanList | ForEach { If ( $sku -eq $_.ObjectId.substring($_.ObjectId.length - 36, 36) ) { $userSku+=$_.SkuPartNumber}} }  

    I would also suggest to clear the variable array if you are going to run the above code for multiple users in the tenant since running the same thing twice will keep appending the value in $userSku value .

    Clear-Variable -Name userSku

    The following is the output of the above modified scripts and you can see the output being stored to the $userSku array variable.


    You can access each element in the array as shown below.


    Hope this helps. In case you have any further queries , please let us know and we will be happy to help . Should the information be helpful , please feel free to accept the post as answer which may help others with similar queries.

    Thank you.


    • Please don't forget to click on 130616-image.png whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
    • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful