Manage licenses with PowerShell in Azure Active Directory!

%3CLINGO-SUB%20id%3D%22lingo-sub-2627903%22%20slang%3D%22en-US%22%3EManage%20licenses%20with%20PowerShell%20in%20Azure%20Active%20Directory!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2627903%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHi%20Azure%20friends%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20article%2C%20I%20will%20describe%20how%20you%20can%20use%20PowerShell%20in%20Azure%20Active%20Directory%20to%20quickly%20get%20information%20about%20licenses.%20I%20have%20summarized%20a%20few%20experiences%20and%20would%20like%20to%20share%20them%20with%20you.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20used%20the%20PowerShell%20ISE%20for%20this%20configuration.%20But%20you%20are%20also%20very%20welcome%20to%20use%20Visual%20Studio%20Code%2C%20just%20as%20you%20wish.%20Please%20start%20with%20the%20following%20steps%20to%20begin%20the%20deployment%20(the%20Hashtags%20are%20comments)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23The%20first%20two%20lines%20have%20nothing%20to%20do%20with%20the%20configuration%2C%20but%20make%20some%20space%20below%20in%20the%20blue%20part%20of%20the%20ISE%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESet-Location%20C%3A%5CTemp%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EClear-Host%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%23We%20need%20the%20cmdlets%3CBR%20%2F%3E%3CSTRONG%3EInstall-Module%20-Name%20AzureAD%20-AllowClobber%20-Force%20-Verbose%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Sometimes%20the%20module%20must%20be%20imported%3CBR%20%2F%3E%3CSTRONG%3EImport-Module%20AzureAD%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Lets%20connect%20to%20the%20Azure%20Active%20Directory%3CBR%20%2F%3E%3CSTRONG%3EConnect-AzureAD%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23What%20licenses%20are%20available%3F%3CBR%20%2F%3E%3CSTRONG%3EGet-AzureADSubscribedSku%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23More%20info%20about%20the%20license%20package%3CBR%20%2F%3E%3CSTRONG%3EGet-AzureADSubscribedSku%20%7C%20Select-Object%20-Property%20ObjectId%2C%20SkuPartNumber%2C%20ConsumedUnits%20-ExpandProperty%20PrepaidUnits%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23What%20is%20included%20in%20the%20license%20package%3CBR%20%2F%3E%3CSTRONG%3EGet-AzureADSubscribedSku%20%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E-ObjectId%2095b14fab-6bbf-4756-94d4-99993dd27f55_05e9a617-0261-4cee-bb44-138d3ef5d965%20%7C%20Select-Object%20-ExpandProperty%20ServicePlans%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23To%20list%20all%20licensed%20users%3CBR%20%2F%3E%3CSTRONG%3EGet-AzureAdUser%20%7C%20ForEach%20%7B%20%24licensed%3D%24False%20%3B%20For%20(%24i%3D0%3B%20%24i%20-le%20(%24_.AssignedLicenses%20%7C%20Measure).Count%20%3B%20%24i%2B%2B)%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%7B%20If(%20%5Bstring%5D%3A%3AIsNullOrEmpty(%20%24_.AssignedLicenses%5B%24i%5D.SkuId%20)%20-ne%20%24True)%20%7B%20%24licensed%3D%24true%20%7D%20%7D%20%3B%20If(%20%24licensed%20-eq%20%24true)%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%7B%20Write-Host%20%24_.UserPrincipalName%7D%20%7D%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23To%20list%20all%20of%20the%20unlicensed%20users%3CBR%20%2F%3E%3CSTRONG%3EGet-AzureAdUser%20%7C%20ForEach%7B%20%24licensed%3D%24False%20%3B%20For%20(%24i%3D0%3B%20%24i%20-le%20(%24_.AssignedLicenses%20%7C%20Measure).Count%20%3B%20%24i%2B%2B)%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%7B%20If(%20%5Bstring%5D%3A%3AIsNullOrEmpty(%20%24_.AssignedLicenses%5B%24i%5D.SkuId%20)%20-ne%20%24True)%20%7B%20%24licensed%3D%24true%20%7D%20%7D%20%3B%20If(%20%24licensed%20-eq%20%24false)%60%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%7B%20Write-Host%20%24_.UserPrincipalName%7D%20%7D%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Do%20users%20have%20a%20usage%20location%3F%20%3CBR%20%2F%3E%3CSTRONG%3EGet-AzureADUser%20%7C%20Select%20DisplayName%2CDepartment%2CUsageLocation%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23We%20select%20a%20user%3CBR%20%2F%3E%3CSTRONG%3E%24User%20%3D%20Get-AzureADUser%20-ObjectId%20fred.prefect%40tomscloud.ch%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23The%20user%20needs%20a%20location%3CBR%20%2F%3E%3CSTRONG%3ESet-AzureADUser%20-ObjectId%20%24User.ObjectId%20-UsageLocation%20CH%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23We%20need%20the%20SKU%20ID%3CBR%20%2F%3E%3CSTRONG%3EGet-AzureADSubscribedSku%20%7C%20Select%20SkuPartNumber%2C%20SkuID%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Create%20the%20AssignedLicense%20object%3CBR%20%2F%3E%3CSTRONG%3E%24Sku%20%3D%20New-Object%20-TypeName%20Microsoft.Open.AzureAD.Model.AssignedLicense%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Set%20the%20SKU%20ID%3CBR%20%2F%3E%3CSTRONG%3E%24Sku.SkuId%20%3D%20%226fd2c87f-b296-42f0-b197-1e91e994b900%22%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Create%20the%20AssignedLicenses%20Object%3CBR%20%2F%3E%3CSTRONG%3E%24Licenses%20%3D%20New-Object%20-TypeName%20Microsoft.Open.AzureAD.Model.AssignedLicenses%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Add%20the%20SKU%3CBR%20%2F%3E%3CSTRONG%3E%24Licenses.AddLicenses%20%3D%20%24Sku%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Setting%20a%20License%20to%20a%20User%3CBR%20%2F%3E%3CSTRONG%3ESet-AzureADUserLicense%20-ObjectId%20%24User.ObjectId%20-AssignedLicenses%20%24Licenses%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Creating%20a%20Custom%20License%3CBR%20%2F%3E%3CSTRONG%3E%24User%20%3D%20Get-AzureADUser%20-ObjectId%20fred.prefect%40tomscloud.ch.ch%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Create%20the%20AssignedLicense%20object%3CBR%20%2F%3E%3CSTRONG%3E%24Sku%20%3D%20New-Object%20-TypeName%20Microsoft.Open.AzureAD.Model.AssignedLicense%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Add%20the%20SKU%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%24Sku.SkuId%20%3D%20%226fd2c87f-b296-42f0-b197-1e91e994b900%22%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Show%20the%20ServicePlans%3CBR%20%2F%3E%3CSTRONG%3EGet-AzureADSubscribedSku%20-ObjectId%2095b14fab-6bbf-4756-94d4-99993dd27f55_05e9a617-0261-4cee-bb44-138d3ef5d965%20%7C%20Select-Object%20-ExpandProperty%20ServicePlans%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Get%20the%20LicenseSKU%20and%20create%20the%20Disabled%20ServicePlans%20object%3CBR%20%2F%3E%3CSTRONG%3E%24Sku.DisabledPlans%20%3D%20%40(%22a23b959c-7ce8-4e57-9140-b90eb88a9e97%22%2C%22aebd3021-9f8f-4bf8-bbe3-0ed2f4f047a1%22)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Create%20the%20AssignedLicenses%20Object%3CBR%20%2F%3E%3CSTRONG%3E%24Licenses%20%3D%20New-Object%20%E2%80%93TypeName%20Microsoft.Open.AzureAD.Model.AssignedLicenses%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Add%20the%20SKU%3CBR%20%2F%3E%3CSTRONG%3E%24Licenses.AddLicenses%20%3D%20%24Sku%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Assign%20the%20license%20to%20the%20user%3CBR%20%2F%3E%3CSTRONG%3ESet-AzureADUserLicense%20-ObjectId%20%24User.ObjectId%20-AssignedLicenses%20%24Licenses%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3ENow%20you%20have%20successfully%20edited%20the%20licenses%20with%20PowerShell%20in%20Azure%20Active%20Directory!%3CBR%20%2F%3ECongratulations!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20hope%20this%20article%20was%20useful.%20Best%20regards%2C%20Tom%20Wechsler%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EP.S.%20All%20scripts%20(%23PowerShell%2C%20Azure%20CLI%2C%20%23Terraform%2C%20%23ARM)%20that%20I%20use%20can%20be%20found%20on%20github!%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftomwechsler%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Ftomwechsler%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2627903%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EHands-on-Labs%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2631077%22%20slang%3D%22en-US%22%3ERe%3A%20Manage%20licenses%20with%20PowerShell%20in%20Azure%20Active%20Directory!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2631077%22%20slang%3D%22en-US%22%3EArticle%20is%20very%20helpful!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2631334%22%20slang%3D%22en-US%22%3ERe%3A%20Manage%20licenses%20with%20PowerShell%20in%20Azure%20Active%20Directory!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2631334%22%20slang%3D%22en-US%22%3EThank%20you!%20It's%20a%20pleasure!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2716336%22%20slang%3D%22en-US%22%3ERe%3A%20Manage%20licenses%20with%20PowerShell%20in%20Azure%20Active%20Directory!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2716336%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20the%20write%20up%20here%20Tom.%3CBR%20%2F%3EWhat%20are%20your%20thoughts%20on%20Microsoft's%20stance%20on%20the%20retirement%20of%20licensing%20via%20MSOnline%20and%20Azure%20AD%20PowerShell%20in%20favour%20of%20the%20Microsoft%20Graph%20PowerShell%20equivalent%20beginning%20on%20June%2030%2C%202022%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-active-directory-identity%2Fmigrate-your-apps-to-access-the-license-managements-apis-from%2Fba-p%2F2464366%22%20target%3D%22_self%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-active-directory-identity%2Fmigrate-your-apps-to-access-the-license-managements-apis-from%2Fba-p%2F2464366%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2717416%22%20slang%3D%22en-US%22%3ERe%3A%20Manage%20licenses%20with%20PowerShell%20in%20Azure%20Active%20Directory!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2717416%22%20slang%3D%22en-US%22%3EIt's%20a%20pleasure!%3CBR%20%2F%3EI%20have%20also%20read%20this%20information%20and%20wait%20once%20until%20there%20is%20more%20information%20about%20it.%3C%2FLINGO-BODY%3E
MVP

 

Hi Azure friends,

 

In this article, I will describe how you can use PowerShell in Azure Active Directory to quickly get information about licenses. I have summarized a few experiences and would like to share them with you.

 

I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):

 

#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE

Set-Location C:\Temp
Clear-Host

#We need the cmdlets
Install-Module -Name AzureAD -AllowClobber -Force -Verbose

 

#Sometimes the module must be imported
Import-Module AzureAD

 

#Lets connect to the Azure Active Directory
Connect-AzureAD

 

#What licenses are available?
Get-AzureADSubscribedSku

 

#More info about the license package
Get-AzureADSubscribedSku | Select-Object -Property ObjectId, SkuPartNumber, ConsumedUnits -ExpandProperty PrepaidUnits

 

#What is included in the license package
Get-AzureADSubscribedSku `
-ObjectId 95b14fab-6bbf-4756-94d4-99993dd27f55_05e9a617-0261-4cee-bb44-138d3ef5d965 | Select-Object -ExpandProperty ServicePlans

 

#To list all licensed users
Get-AzureAdUser | ForEach { $licensed=$False ; For ($i=0; $i -le ($_.AssignedLicenses | Measure).Count ; $i++)`
{ If( [string]::IsNullOrEmpty( $_.AssignedLicenses[$i].SkuId ) -ne $True) { $licensed=$true } } ; If( $licensed -eq $true)`
{ Write-Host $_.UserPrincipalName} }

 

#To list all of the unlicensed users
Get-AzureAdUser | ForEach{ $licensed=$False ; For ($i=0; $i -le ($_.AssignedLicenses | Measure).Count ; $i++)`
{ If( [string]::IsNullOrEmpty( $_.AssignedLicenses[$i].SkuId ) -ne $True) { $licensed=$true } } ; If( $licensed -eq $false)`
{ Write-Host $_.UserPrincipalName} }

 

#Do users have a usage location?
Get-AzureADUser | Select DisplayName,Department,UsageLocation

 

#We select a user
$User = Get-AzureADUser -ObjectId fred.prefect@tomscloud.ch

 

#The user needs a location
Set-AzureADUser -ObjectId $User.ObjectId -UsageLocation CH

 

#We need the SKU ID
Get-AzureADSubscribedSku | Select SkuPartNumber, SkuID

 

#Create the AssignedLicense object
$Sku = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense

 

#Set the SKU ID
$Sku.SkuId = "6fd2c87f-b296-42f0-b197-1e91e994b900"

 

#Create the AssignedLicenses Object
$Licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses

 

#Add the SKU
$Licenses.AddLicenses = $Sku

 

#Setting a License to a User
Set-AzureADUserLicense -ObjectId $User.ObjectId -AssignedLicenses $Licenses

 

#Creating a Custom License
$User = Get-AzureADUser -ObjectId fred.prefect@tomscloud.ch.ch

 

#Create the AssignedLicense object
$Sku = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense

 

#Add the SKU

$Sku.SkuId = "6fd2c87f-b296-42f0-b197-1e91e994b900"

 

#Show the ServicePlans
Get-AzureADSubscribedSku -ObjectId 95b14fab-6bbf-4756-94d4-99993dd27f55_05e9a617-0261-4cee-bb44-138d3ef5d965 | Select-Object -ExpandProperty ServicePlans

 

#Get the LicenseSKU and create the Disabled ServicePlans object
$Sku.DisabledPlans = @("a23b959c-7ce8-4e57-9140-b90eb88a9e97","aebd3021-9f8f-4bf8-bbe3-0ed2f4f047a1")

 

#Create the AssignedLicenses Object
$Licenses = New-Object –TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses

 

#Add the SKU
$Licenses.AddLicenses = $Sku

 

#Assign the license to the user
Set-AzureADUserLicense -ObjectId $User.ObjectId -AssignedLicenses $Licenses


Now you have successfully edited the licenses with PowerShell in Azure Active Directory!
Congratulations!

 

I hope this article was useful. Best regards, Tom Wechsler

 

P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github! https://github.com/tomwechsler

4 Replies
Article is very helpful!
Thank you! It's a pleasure!

Thanks for the write up here Tom.
What are your thoughts on Microsoft's stance on the retirement of licensing via MSOnline and Azure AD PowerShell in favour of the Microsoft Graph PowerShell equivalent beginning on June 30, 2022?

 

https://techcommunity.microsoft.com/t5/azure-active-directory-identity/migrate-your-apps-to-access-t... 

It's a pleasure!
I have also read this information and wait once until there is more information about it.