SOLVED

MS Microsoft Graph PowerShell SDK to assign licenses in bulk from a csv file

Copper Contributor

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?

 

 

9 Replies
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/
best response confirmed by AdamAtTheMuseum (Copper Contributor)
Solution

Hi @AdamAtTheMuseum 

 

https://blog.icewolf.ch/archive/2021/11/29/hinzufugen-und-entfernen-von-m365-lizenzen-mit-microsoft-... 

 

Figure Out your SKUID's here

https://learn.microsoft.com/en-us/azure/active-directory/enterprise-users/licensing-service-plan-ref... 

 

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

Thanks everyone I have managed to figure it out with your help!

@Andres Bohren 


my CSV files is formatted like this

email

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

@emansqi

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 

Thanks for responding @Andres Bohren 

 

The licenses is working is use it for one email address at a time.  that I'm getting error at is on -UserId

 

errors says cannot bind because UserID is empty string. I can't seem to pull the list of email addresses from the imported CSV. when I run the command using the same address on the CSV at one time, it works.

 

 

@emansqi 

 

$CSV = Import-Csv C:\changelicense\batch01.csv #-Delimiter ";"
#Check what's in the CSV by selecting the first Item of the Array

$CSV[0]

ForEach ($User in $CSV) {
$UPN = $User.email
#Debug

Write-Host "Working on: $UPN"

Set-MgUserLicense -UserId $UPN -AddLicenses @() -RemoveLicenses @{SkuId = $($Mf3Sku.SkuId)}
Set-MgUserLicense -UserId $UPN -AddLicenses @{SkuId = $($0e3Sku.SkuId)} -RemoveLicenses @()
}


Regards

Andres

Hi @Andres Bohren,

Can you help me to convert my script to the new version
$userName = "SampleUsername"
$password = "Password"
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $securePassword
Connect-MsolService -Credential $credential
$users = Import-Csv D:\O365\bulk2.csv
Import-Csv -Path D:\O365\bulk2.csv | ForEach-Object {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -LicenseAssignment $_.AccountSkuId -Password $_.Password -UsageLocation $_.UsageLocation }

I'm not really good with this kind of script, I really appreciate if you can help me :)

Thanks!
Pol

@Pol18 

im in the same ballpark

 

$CsvFilePath = "C:\Users\d.kritikos\Downloads\CSV-Files\A1StudentsTestList.csv"
$A1SkuId = Get-MgSubscribedSku -All| Where-Object SkuPartNumber -eq STANDARDPACK_STUDENT
$CsvData = Import-Csv $CsvFilePath
$Userlist = foreach ($CsvRow in $CsvData) {
$Upn = $CsvRow.UserPrincipalName
Get-MgUser -Filter "UserPrincipalName eq '$Upn'"`
-ConsistencyLevel eventual -CountVariable licensedUserCount -All `
-Select UserPrincipalName,DisplayName,AssignedLicenses
}
foreach($user in $Userlist)
{
$user = Set-MgUserLicense -UserId $user.UserPrincipalName -RemoveLicenses @($A1SkuId.SkuId) -AddLicenses @{}
}

 

returns error

Set-MgUserLicense : Cannot convert the literal '' to the expected type 'Edm.Guid'.
At line:3 char:5
+ $user = Set-MgUserLicense -UserId $user.UserPrincipalName -Remove ...

 

I would love to see a working example of this too.

1 best response

Accepted Solutions
best response confirmed by AdamAtTheMuseum (Copper Contributor)
Solution

Hi @AdamAtTheMuseum 

 

https://blog.icewolf.ch/archive/2021/11/29/hinzufugen-und-entfernen-von-m365-lizenzen-mit-microsoft-... 

 

Figure Out your SKUID's here

https://learn.microsoft.com/en-us/azure/active-directory/enterprise-users/licensing-service-plan-ref... 

 

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

View solution in original post