May 23 2023 05:52 AM
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?
May 23 2023 07:55 AM
May 23 2023 12:50 PM
Solution
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
May 24 2023 07:33 AM
Jun 04 2023 05:10 AM - edited Jun 04 2023 05:43 AM
my CSV files is formatted like this
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
Jun 04 2023 06:25 AM
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
Jun 04 2023 08:10 AM
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.
Jun 04 2023 02:20 PM - edited Jun 04 2023 02:20 PM
$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
Aug 02 2023 01:47 AM
Aug 17 2023 05:32 AM
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.
May 23 2023 12:50 PM
Solution
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