Forum Discussion
MS Microsoft Graph PowerShell SDK to assign licenses in bulk from a csv file
Hello
I have a .csv file with over 400 users in it in the form of the email address of each user.
I need to assign a Microsoft 365 license to each user as a one-off process. They currently have three separate licenses, Windows 10, EMS and Office 365.
I used to be able to use msol cmdlets but these have been deprecated and I can no longer use them.
the cmdlets were Get-Content
then
ForEach
Set-MsolUserLicence
etc etc...
But now I have to use MS Graph Powershell SDK (which I have installed) and for the life of me I cannot work out how to do this.
I can see its Set-MgUserLicense -AddLicenses but all the examples I see are to set a licence to just one user...
I want to read my list of users and do a For-Each on them but I cant see how I can do that in Microsoft Graph Powershell SDK.
Can anyone help?
Figure Out your SKUID's here
Connect-MgGraph -Scopes User.ReadWrite.All, Directory.ReadWrite.All
$CSV = Import-CSV -Path C:\Test\users.csv #-Delimiter ";"
ForEach ($User in $CSV)
{
$UPN = $User.Email
Set-MgUserLicense -UserId $UPN -AddLicenses @{SkuId = 'cb10e6cd-9da4-4992-867b-67546b1db821'} -RemoveLicenses @()
}
Regards
Andres
- Andres-BohrenSteel Contributor
Figure Out your SKUID's here
Connect-MgGraph -Scopes User.ReadWrite.All, Directory.ReadWrite.All
$CSV = Import-CSV -Path C:\Test\users.csv #-Delimiter ";"
ForEach ($User in $CSV)
{
$UPN = $User.Email
Set-MgUserLicense -UserId $UPN -AddLicenses @{SkuId = 'cb10e6cd-9da4-4992-867b-67546b1db821'} -RemoveLicenses @()
}
Regards
Andres
- AdamAtTheMuseumCopper ContributorThanks everyone I have managed to figure it out with your help!
- emansqiCopper Contributor
my CSV files is formatted like thisemail
email address removed for privacy reasons
email address removed for privacy reasons
email address removed for privacy reasons
email address removed for privacy reasons
I tried using your recommendation but my script is below$CSV = Import-Csv C:\changelicense\batch01.csv #-Delimiter ";"
ForEach ($User in $CSV) {
$UPN = $User.email
Set-MgUserLicense -UserId $UPN -AddLicenses @() -RemoveLicenses @{SkuId = $Mf3Sku.SkuId}
Set-MgUserLicense -UserId $UPN -AddLicenses @{SkuId = $0e3Sku.SkuId} -RemoveLicenses @()
}but I'm getting
Cannot convert the literal 'System.Collections.Hashtable' to the expected type 'Edm.Guid'.
what am I'm missing? thanks in advance
- Andres-BohrenSteel Contributor
II think the CSV is fine
Try to replace the SKU with the ID as a String "skuid"
Or add $() to your SKUId
Example:
Set-MgUserLicense -UserId $UPN -AddLicenses @{SkuId = $($0e3Sku.SkuId) }Regards
Andres
- You will need to build the script but using Graph SDK PowerShell, I recommend this resource to start getting use to it to have the confidence to create it and use it
https://practical365.com/microsoft-365-license-graph-sdk/