question

freumich-6619 avatar image
0 Votes"
freumich-6619 asked shashishailaj commented

[Powershell] AzureAD LicencePlan SKU to variable

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://docs.microsoft.com/en-us/microsoft-365/enterprise/view-account-license-and-service-details-with-microsoft-365-powershell?view=o365-worldwide

$userUPN="AlF@M365EDU737345.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) ) { 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!

azure-ad-connect
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

shashishailaj avatar image
1 Vote"
shashishailaj answered shashishailaj commented

@freumich-6619 ,
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.

187509-image.png

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

187605-image.png

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]: /answers/storage/attachments/187545-image.png


image.png (35.8 KiB)
image.png (34.0 KiB)
image.png (18.1 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you @shashishailaj to answer my question.
It works like expected!
Have a nice Day!

0 Votes 0 ·

@freumich-6619 , Glad to know that it worked . Thanks.

1 Vote 1 ·