Using the Microsoft.Graph PowerShell module to assign Office 365 licenses (PowerShell Core)

2 minute read

I’ve been working on automating the onboarding process at my new job using PowerShell, but specifically attempting to do as much of it as possible in PowerShell 7. Right now this means I’m importing the Exchange Online Management module -UseWindowsPowerShell, but I expect to be able to drop that when they finish V2.

Anyways, since we use Office 365 one of the things that came up was, of course, the Azure AD PowerShell module. It is not Core compatible, nor does it appear that it will ever be, so I did some digging and found the Microsoft.Graph PowerShell module. That is Core compatible and, so far, it appears that it can do all of the things I need it to!

Assigning licenses

To assign licenses using Microsoft.Graph, make sure you’ve imported at least the Microsoft.Graph.Users.Actions module and you’ve connected to the Graph API using Connect-Graph. Once you’ve done that, pick out a user that has the licenses you want to copy:

$mgUser = Get-MgUser -UserId 'anthony@howell-it.com'

And yes, I’m still referencing my consulting email even though I’m no longer consulting. I still have an O365 subscription, so why not I say!

With that user, we can look at the AssignedLicenses property:

PS> $mgUser.AssignedLicenses

DisabledPlans SkuId
------------- -----
{}            6fd2c87f-b296-42f0-b197-1e91e994b900
{}            a403ebcc-fae0-4ca2-8c8c-7a907fd6c235
{}            f30db892-07e9-47e9-837c-80727f46fd3d

This should look familiar if you’ve used the AzureAD module because it is the same exact output! If you were interested in seeing some more details about what those license SkuId’s were, you could also look at:

Get-MgUserLicenseDetail -UserId 'anthony@howell-it.com'

That’ll give you some different info, here’s what my user looks like:

Id   SkuId                                SkuPartNumber
--   -----                                -------------
<id> 6fd2c87f-b296-42f0-b197-1e91e994b900 ENTERPRISEPACK
<id> a403ebcc-fae0-4ca2-8c8c-7a907fd6c235 POWER_BI_STANDARD
<id> f30db892-07e9-47e9-837c-80727f46fd3d FLOW_FREE

So with that AssignedLicenses data, we can simply assign those same licenses to another user with:

Set-MgUserLicense -UserId 'anthony2@howell-it.com' -AddLicenses $mgUser.AssignedLicenses -RemoveLicenses @()

A couple of things to note here, in the current version of the Microsoft.Graph.Users.Actions module, you need to pass an empty arround to -RemoveLicenses, otherwise you will get an error:

Set-MgUserLicense_AssignExpanded: One or more parameters of the function import 'assignLicense' are missing from the request payload. The missing parameters are: removeLicenses.

This is because the API still expects that property to exist, but the cmdlet is not sending it. So we can tell it not to remove any licenses with an empty array @(). If you were going to remove licenses, you would pass an array of license SkuIds.

Leave a Comment