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
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
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.