December 6, 2025

Switching M365 Licensing using PowerShell

Switching M365 Licensing

PowerShell can automate several tasks that you might find yourself in M365 Administration. You may find yourself in an organization in the need of Switching M365 Licensing using PowerShell. At least I would use PowerShell. Changing licensing requires you to remove the older license and add the new licensing. Try manually doing that for hundreds of accounts…not cool. Using PowerShell is a lot faster. I will show you how to get it done in no time.

Figure out the SKU for your Licenses

When I say licenses, I mean the ones you currently have and the ones you want to replace them with. There are 2 ways to do this. If you don’t have a lot of licensing with Microsoft you can just look it up by going to this article by Microsoft. If you have a lot of different licensing or you would just like a handy CSV of what you currently have, go to this site. It has a nice PowerShell Script.

Switching M365 Licensing using PowerShell Example

In this example I am going to use an example of a company that is upgrading their licensing from M365 Business Premium, with Teams Phone Standard and Exchange Online Plan 2 to M365 E5, which includes all the old licensing plus more.

First things first, make sure you have enough of the new licensing. Go to licenses in the admin portal and make sure you have enough. The script will err after it runs out of licenses to switch. At that point you can also go back to the portal and see what users were missed and manually switch them.

The User List

For Switching M365 Licensing using PowerShell you need to create a CSV File that has the UPN of the user and their Display Name. the script will need it. In licenses you can go to the exact license and it will show all the users licensed to it. You can download the list as a CSV and it will help you create the list you need in the script.

The License SKU’s

Make a list of old SKU’s and the new ones. In my example I have the following:

Old – SPB (M365 Business Premium),MCV (Teams Phone Standard),EXCHANGEENTERPRISE(Exchange Online Plan 2)

New – SPE_E5 (M365 E5)

The Script

Here is the PowerShell Script that removes the old licenses and adds the new:

#Path to UPN File #

$CSVPathUPN = "C:\Path\To\CSV\usersexchplan2.csv"


##Run Script##

##Try import UPN CSV file##

Write-Host Importing CSV

try {
    $UPNUsers = import-csv $CSVPathUPN -ErrorAction stop
}
catch {
    throw "Error importing CSV: $($_.Exception.Message)"
    break
}

$busprem = Get-MgSubscribedSku -All | Where SkuPartNumber -eq ' SPB '
$teamsphone = Get-MgSubscribedSku -All | Where SkuPartNumber -eq ' MCV '
$exchplan2 = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'EXCHANGEENTERPRISE'

foreach ($UPNUser in $UPNUsers) {
	
$Uname = $UPNUser.UPName
$Dname = $UPNUser.displayName

# Unassign Bus Prem and Teams Phone Standard

Write-Host Removing Bus Premium, Teams Phone Standard Licensing and Exchange Online for $Dname ....
Set-MgUserLicense -UserId $Uname -AddLicenses @{} -RemoveLicenses @($busprem.SkuId)
Set-MgUserLicense -UserId $Uname -AddLicenses @{} -RemoveLicenses @($teamsphone.SkuId)
Set-MgUserLicense -UserId $Uname -AddLicenses @{} -RemoveLicenses @($exchplan2.SkuId)
}

Write-Host Need to Pause so removed licenses can sync before adding the new ones

Pause

$e5Sku = Get-MgSubscribedSku -All | Where SkuPartNumber -eq 'SPE_E5'

foreach ($UPNUser in $UPNUsers) {

$Uname = $UPNUser.UPName
$Dname = $UPNUser.displayName
# Assign E5	

Set-MgUserLicense -UserId $Uname -AddLicenses @{SkuId = $e5Sku.SkuId} -RemoveLicenses @()

Write-Host Added e5 Licensing for $Dname ....

}

Write-Host All done

You Need To Test!

Please do not run this script with your entire user list. Take a small sample. If something goes wrong you only have to work with a few users and not hundreds.

This example script is great for installations where most users have the same licensing but in cases where the user licensing in all over the place or they use group-based licensing, you will have to tweak the script. In a later post I will show you how to deal with assigning / removing licensing in a group-based scenario.

Avatar photo

I am an IT professional with over twenty five years experience in the field. I have supported thousands of users over the years. The organizations I have worked for range in size from one person to hundreds of people. I have performed support from Help Desk, Network / Cloud Administration, Network Support, Application Support, Implementation and Security.

Share: Facebook Twitter Linkedin

Comments are closed.