Set-ACMEO365UserLicense function in PowerShell

Provisioning O365 user Licenses

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!

Set-ACMEO365UserLicense function in PowerShell
Set-ACMEO365UserLicense function in PowerShell

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!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.