Microsoft 365 - Assign licenses with PowerShell to users
During the working week i get a lot of questions about assigning licenses (mostly the Microsoft Teams Phone Standard license) to users in bulk. This can be easily resolved with PowerShell.
First logon to MSOnline, if you don’t have this module installed use: Install-Module -name MSOnline
Connect-MsolService
Let’s see your active licenses
Get-MsolAccountSku
In order to assign a license to one of your users use the following cmdlet
Set-MsolUserLicense -UserPrincipalName user@contoso.com -AddLicenses tenant:mcoev
In this example we’ve assigned the Microsoft Teams Phone Standard license to the user.
Bulk mode
If you want to do this for multiple accounts you can do this by creating a script. First create a csv file with a column header: UPN List all accounts below with the UPN address who will need the license.
Save script below as Set-SKULicense.ps1 and change the $FilePath to the correct filename. Now just run the file and copy/paste the license in the prompt you want to assign.
$FilePath = "c:\temp\users.csv"
Import-Module MSOnline
Connect-MsolService
#list all available SKUs
Get-MsolAccountSku | ft AccountSkuId
Write-Host -ForegroundColor Cyan "[OK] waiting for 10 seconds"
Start-Sleep 10
Write-Host -ForegroundColor Cyan "Please provide the right tenant:sku from above list"
$LicenseSKU = Read-Host "Please enter tenant:sku"
#import the CSV file
Import-Csv $FilePath | ForEach-Object {
Try{
Set-MsolUserLicense -UserPrincipalName $_.UPN -AddLicenses $LicenseSKU
Write-Host "License $LicenseSKU assigned to "$($_.UPN)"" -ForegroundColor Green
}
catch {
Write-Host "[ERROR] License not assigned to user "$($_.UPN)"" -ForegroundColor Red
}
}
# disconnect session
[Microsoft.Online.Administration.Automation.ConnectMsolService]::ClearUserSessionState()