Assign Removing Licenses from users in bulk. - mattnovitsch/M365 GitHub Wiki
My scipt is located here. My script is going to target all users with Flow_Free and remove that then add ATA licenses to those accounts.
Note: You will need to change mattazurelabs:Flow_Free and mattazurelabs:ATA to your organization tenant name and SKUs.
To find your license SKUs, please reference this document: Product names and service plan identifiers for licensing - Azure AD - Microsoft Entra | Microsoft Learn
<#
#Install Modeule for service
install-module MSOnline
#Connect to M365 services
Connect-MsolService
#>
#Find your current Account License SKUs
Get-MsolAccountSku
#Assign location to each user.
Get-MsolUser -All | where {$.UsageLocation -eq $null} | Set-MsolUser -UsageLocation US
#Get the users that have the current license you need to swap out.
$Users = Get-MsolUser -all | Where {$.Licenses.AccountSkuId -contains "mattazurelabs:Flow_Free"} | Select UserPrincipalName
Foreach ($User in $Users)
{
write-host "Adding mattazurelabs:WIN_DEF_ATP License to uesr:" $User.UserPrincipalName
Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -AddLicenses "mattazurelabs:ATA"
write-host "Removing mattazurelabs:WIN_DEF_ATP License from uesr:" $User.UserPrincipalName
Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -RemoveLicenses "mattazurelabs:Flow_Free"
}
In my example, I am looking for anyone that has PowerAutomate license and removing it and adding in Defender for Identity. After I run the script you can see there are no users in the Power Automate Free license.