Forum Discussion

VBardV's avatar
VBardV
Copper Contributor
Nov 23, 2023

How do i change user's exchange license in bulk using csv?

Import-Csv "C:\bulk.csv" | ForEach-Object {Set-MgUserLicense -UserId $_.UPN -RemoveLicenses @("xxx") -AddLicenses @{}}

Import-Csv "C:\bulk.csv" | %{Set-MgUserLicense -UserId $_.UPN -AddLicenses @{} -RemoveLicenses @(SkuId = "xxx")}

 

+ CategoryInfo : InvalidOperation: ({ UserId = kiti...ionJsonSchema }:<>f__AnonymousType8`2) [Set-MgUserLicense_AssignExpanded1], RestException`1
+ FullyQualifiedErrorId : Request_ResourceNotFound,Microsoft.Graph.PowerShell.Cmdlets.SetMgUserLicense_AssignExpanded1
Set-MgUserLicense : Resource 'email address removed for privacy reasons ' does not exist or one of its queried reference-property objects are not present.
At line:1 char:36
+ ... lk.csv" | %{Set-MgUserLicense -UserId $_.UPN -RemoveLicenses @("xxx ...

 

I am having hundreds of users pending since Set-MsolUserLicense is not usable anymore.

Kindly help.

7 Replies

    • VBardV's avatar
      VBardV
      Copper Contributor

      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.

      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        VBardV 

         

        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

Resources