Forum Discussion
MS Microsoft Graph PowerShell SDK to assign licenses in bulk from a csv file
- May 23, 2023
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
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.
- Andres-BohrenJun 04, 2023Iron Contributor
$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
#DebugWrite-Host "Working on: $UPN"
Set-MgUserLicense -UserId $UPN -AddLicenses @() -RemoveLicenses @{SkuId = $($Mf3Sku.SkuId)}
Set-MgUserLicense -UserId $UPN -AddLicenses @{SkuId = $($0e3Sku.SkuId)} -RemoveLicenses @()
}
RegardsAndres
- Pol18Aug 02, 2023Copper ContributorHi 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- DanielKritikosAug 17, 2023Copper Contributor
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.