Forum Discussion

DamienFR68's avatar
DamienFR68
Copper Contributor
Oct 17, 2023

Attributing Microsoft 365 licenses to a batch of users

Hello, I am trying to add Microsoft 365 licenses to a batch of users using a csv file. The script I use currently is the following:   $InputFile = "c:\temp\comptes.csv" [array]$Users = Import-CS...
  • LainRobertson's avatar
    Oct 17, 2023

    DamienFR68 

     

    The error is fairly straight forward. Have a read of the following article:

     

     

    This isn't an PowerShell issue, but rather than Azure licencing issue.

     

    If you can't figure out which user is causing the error, try slightly changing your script to something like this instead:

     

    $InputFile = "c:\temp\comptes.csv"
    [array]$Users = Import-CSV $InputFile
    ForEach ($User in $Users)
    {
        try
        {
            $License = Set-MgUserLicense -UserId $User.UPN -Addlicenses @{SkuId = '18181a46-0d4e-45cd-891e-60aabd171b4e'} -RemoveLicenses @() -ErrorAction:Stop;
        }
        catch
        {
            throw "Script failed while attempting to set the licence for $($User.UPN). Aborting.";
        }
    }

     

    Cheers,

    Lain

Resources