At the end of the encore webinar I led today, we had composed a little function. Well I was doing the typing and talking, but thanks to all who attended the webinar live!

One of the lovely embarrassing things that can come up in a live webinar did. I was typing this function (and about eight prior lines apparently) toward the end of the webinar. I had a typo on line 91. Instead of $oneLicense I had $license as the parameter to the Add method. So the function didn’t work when I was doing the demo. Sorry about that. But as soon as I people departed and I ended the Zoom Webinar session, I saw the problem. I fixed it before making the screenshot above and the function works.
Note that there are a few other little magic ingredients needed before running the function, such as logging into AzureAD with user admin or global admin privileges using Connect-AzureAD. Please let me know if you have any questions or comments on this.
Here is a text version of that function definition.
function Set-ACMEO365UserLicense {
param( $Name, $TemplateName = "Samantha" )
$template = Get-AzureADUser -SearchString $TemplateName
# or could create license objects from scratch
$licenses = New-Object Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.AddLicenses = @() # empty array (list)
$template.AssignedLicenses | ForEach-Object { # walk through the existing "template" user's licenses
$oneLicense = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$oneLicense.SkuId = $_.SkuId # copies one product code from existing user
$licenses.AddLicenses.Add( $oneLicense ) # void Add(Microsoft.Open.AzureAD.Model.AssignedLicense item)
}
$u = Get-AzureADUser -SearchString $Name
Set-AzureADUserLicense -ObjectId $u.ObjectId -AssignedLicenses $licenses
}
I hope this is helpful. Thank you!