Forum Discussion
How do i change user's exchange license in bulk using csv?
Hi.
You've used the wrong format for -RemoveLicenses, as it is supposed to be a string array, not a HashTable.
Example
Strictly-speaking, the error encompasses two reasons for failure, however, I've gone with the second reason for this reply since that's definitely a problem within your second command.
Cheers,
Lain
Hi Lain,
Warm Greetings,
My apologies not sure how i got it the other way around. I've corrected the syntax and i'm able to remove a license for one user but the bulk command is not working.
Import-Csv "C:\bulk.csv" | %{Set-MgUserLicense -UserId $_.UPN -AddLicenses @{} -RemoveLicenses @("XXX")}
Error as per post below:
+ CategoryInfo : InvalidOperation: ({ UserId = rang...ionJsonSchema }:<>f__AnonymousType8`2) [Set-MgUserLicense_AssignExpanded1], RestException`1
+ FullyQualifiedErrorId : Request_ResourceNotFound,Microsoft.Graph.PowerShell.Cmdlets.SetMgUserLicense_AssignExpanded1
Set-MgUserLicense : Resource 'email address ' does not exist or one of its queried reference-property objects are not present.
At line:1 char:50
+ ... ach-Object {Set-MgUserLicense -UserId $_.UPN -AddLicenses @{} -Remove ...
Kindly advise.
- LainRobertsonNov 23, 2023Silver Contributor
The only other thing I can see - and it's subtle - is that within your error message, the userPrincipalName has a trailing space, which points back to your CSV file being the problem.
The userPrincipalName must be completely accurate. Leading or trailing spaces will cause the user to not be found.
Cheers,
Lain
- VBardVNov 23, 2023Copper ContributorHi Lain,
Warm Greetings,
The $_.UPN variable for the csv header was working before. The bulk command was working before but I'm not sure why it is not working now. Has the bulk command been changed / depreciated as well?- LainRobertsonNov 23, 2023Silver Contributor
The command is the same, whether you run it singularly or in a loop. There is no explicit bulk command as such
The script itself is fine. I expect the issue is with your CSV file.
Try running the following example script (based on your own script) and see if the userPrincipalName values in between the square brackets look okay - specifically keeping an eye out for any leading or trailing spaces (which will push the surrounding square brackets outwards in the warning message):
Example
Import-Csv "C:\bulk.csv" | Select-Object -First 5 | % { Write-Warning -Message "Processing user [$($_.UPN)]"; Set-MgUserLicense -UserId $_.UPN -AddLicenses @{} -RemoveLicenses @("XXX") }Cheers,
Lain
- LainRobertsonNov 23, 2023Silver Contributor
The only other meaning that error encompasses is that the user provided in the $_.UPN reference does not exist.
Cheers,
Lain