December 10, 2025

A Simple Script for M365 Licenses 

You need to juggle M365 Licenses, but you need to see who has them first? It seems simple, it is, and you can write a Script for M365 Licenses, but you will need to do a little preparation first. 

Why Would you Need a Script for M365 Licenses 

That is a very good question. A good example is that you are switch licensing vendors and you would like to see how many licenses you need to switch out. Once you know who has this license you can get a list and then switch them out. 

I can give you a piece of advice. Add new licenses first and make sure they work before you remove the old licenses! Even if the old licenses sit expired, the new ones must be added first. I will write an article in the future of what happens if you do it backwards LOL. 

A script for M365 licenses will help with this. 

What you Need to Write the Script 

You will need 4 Things. 

  1. A csv file with the group of users you would like to check. This CSV will include three columns labelled UPName, DisplayName and Object ID 
  1. The SKU ID of the Microsoft product you want to Check 
  1. A PowerShell script to check the SKU 
  1. A PowerShell script to deal with the SKU (in this case, remove) 

Create a CSV for Your Script for M365 Licenses 

This is an example CSV that you would use for your script. Name it Sample.csv so it is the same file that is referenced in the script.

To get this information, you will need to go to your Entra Portal and under all users, click download users:

You can use this info to create your sample.csv.

Get the SKU ID of the M365 License you Want to Query 

You will need to connect to Microsoft Graph and run the command Get-MgSubscribedSku: 

Connect-Graph -Scopes Organization.Read.All 

Get-MgSubscribedSku | Select -Property Sku*, ConsumedUnits -ExpandProperty PrepaidUnits | Format-List  

You will get a listing of all licensed products in your tenant. From there you can get the SkuId: 

SkuId : 06ebc4ee-1bb5-47dd-8120-11324bc54e06 

SkuPartNumber : SPE_E5 

In this example, we are querying the SkuID for the now discontinued E5 licensing. It has been happening for awhile but it might be hitting you now because your current agreement needs to be renewed. You will now need to copy the ID into a Script for M365 Licenses. Now that you have your CSV and License to query, it is now time for the Script for M365 Licenses. 

Script to Check for the SKU 

# Connect to Microsoft 365 

Connect-MgGraph -Scopes User.Read.All, Organization.Read.All 

 
#Path to UPN File # 


$CSVPathUPN = ".\sample.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 

} 


foreach ($UPNUser in $UPNUsers) { 

 

$Uname = $UPNUser.UPName 

$Dname = $UPNUser.displayName 

$ObjID = $UPNUser.ObjectID 


# Checks if they have SPE5 


# Define the license SKU you're checking for (e.g., "E5" or the SKU ID) 

$Sku = "06ebc4ee-1bb5-47dd-8120-11324bc54e06" 

 
# Get the user's license details 

$CurrentLicenses = Get-MgUserLicenseDetail -UserId $Uname | Select-Object -ExpandProperty SkuId 

 

# Check if the specified license is assigned 

if ($Sku -in $CurrentLicenses) { 

    Write-Host "User '$Dname' has license: $Sku" 

} else { 

    Write-Host "User '$Dname does not have license: $Sku" 

} 

 

} 

Write-Host All done 



The script will cycle through each user and tell you whether that user has that license. 
 

Script to Remove for the SKU 

Now that you know which user(s) have the licenses, you can compile your CSV above and run the following script to remove the them: 


##Connect to Azure and MSGraph modules## 

Connect-MgGraph -Scopes User.ReadWrite.All, Organization.Read.All 

 

#Path to UPN File # 

 

$CSVPathUPN = ".\sample.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 

} 

 

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

 

foreach ($UPNUser in $UPNUsers) { 

 

$Uname = $UPNUser.UPName 

$Dname = $UPNUser.displayName 

$ObjID = $UPNUser.ObjectID 

 

# Remove SPE5 

 

Set-MgUserLicense -UserId $Uname -RemoveLicenses @($f1Sku.SkuId) -AddLicenses @() -ErrorAction Stop 

 

Write-Host Remove Legacy E5 Licensing for $Dname .... 

 

} 

 

Write-Host All done

This is a good way to write a Script for M365 Licenses!

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.