MS License Swap Script

Microsoft licensing is a MESS. Its muddy. Its convoluted. Its headache-inducing. Needless to say, changing licensing can cause headaches if its not automated. Here is a simple, quick-and-dirty, script to accomplish a swap from one license to another. I’ll break it down below for those who want to learn, and for those more interested in the TL;DR, here it is from my GitHub page: Convert-M365License

Let’s start off by importing the Microsoft.Graph module and connecting to the tenant. Remember, when you kick this script off, you will be prompted for tenant level credentials. Ensure the account you are using has the proper permissions to apply licenses.

Import-Module Microsoft.Graph
Connect-Graph -Scopes User.ReadWrite.All, Organization.Read.All

Next we are defining which licenses we want to ADD and REMOVE.

$AddSku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'O365_BUSINESS_PREMIUM'
$RemoveSku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'O365_BUSINESS_STANDARD'

You can find the SKUs by running this cmdlet and copy/pasting the desired license in the code above.

Get-MgSubscribedSku -All | Select-Object SkuPartNumber

Now for the messy piece. There’s probably a much better way to do this, but I was on a time crunch. Let’s get all user objects in the tenant and select only the objects that have the desired license. In my case, it was Business Standard licensing.

$AllUsers = Get-MgUser -All | Select-object Id, Mail
$FullList =@()
foreach($user in $AllUsers){
    $License = Get-MgUserLicenseDetail -UserId $user.Id
    $Properties = [pscustomobject]@{
        UPN = $user.Mail
        UPID = $user.Id
        SkuID = $License.SkuId
        SkuPartNo = $License.SkuPartNumber
    }
    $FullList += $Properties
}
$FinalList = $FullList | Where-Object SkuPartNo -eq 'O365_BUSINESS_STANDARD'

Lastly, lets make our license swap. I’m always super cautious when running production impacting changes, so I left the -WhatIf switch in there. Remove it once you have fully tested and are ready for production.

Foreach($item in $FinalList){
Set-MgUserLicense -UserId $item.UPID -AddLicenses @{SkuId = $AddSku.SkuId} -whatif
Set-MgUserLicense -UserId $item.UPID -RemoveLicenses @{SkuId = $Sku.SkuId} -whatif
}

Feel free to change any variable names or clean this up. Again, this was quick and dirty considering I had a short timespan to get my licenses swapped over.

I hope this helps some SysAdmin in a pinch! Check out my GitHub for more utility scripts! GitHub

As always remember: This script is provided “as is” without any warranty of any kind, express or implied. Use it at your own risk. The authors and contributors are not responsible for any damage, data loss, or other issues that may arise from using this software. You are solely responsible for any actions taken based on this code.