none
Office 365 Bulk UPN Update RRS feed

  • Question

  • Hello,

    I have a list of users in a CSV, has current UPN of "ID@domain.com", and ObjectID of each user. Each user has been added to Office 365 with their numeric ID as their logon and UPN name, but we are wanting to change that to Firstname.Lastname@subdomain.domain.com. As you can see, the UPN has to be changed to subdomain.domain.com, rather than domain.com.

    The Firstname.Lastname@subdomain.domain.com has to be their primary email address (SMTP, not smtp), and it also has to become their Office 365 login/username.

    So far I have this:

    Import-Module -Name MSOnline
    $Cred = Get-Credential
    Connect-MsOnline -Credential $Cred
    $O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $MyCredential -Authentication Basic -AllowRedirection 
    Import-PSSession $O365Session -AllowClobber 
    
    #CSV from export which has been filtered to only contain student accounts. This will add the value of accounts to their department value.
    Import-Csv "c:\users\user\data.csv" | foreach { Set-MsolUser -Department Test}
    
    #This is receiving all the user objects via Azure AD, and filtering by the department value. The new UPN is then applied to users based on that filter
    Get-Msoluser -Department "Test" | foreach { Set-MsolUserPrincipalName -UserPrincipalName $_.UserPrincipalName -NewUserPrincipalName ($_.firstname + "." + $_.lastname + '@subdomain.domain.com')
    }

    However this is the error that is returned:

    Set-MsolUserPrincipalName : Input exceeded the maximum allowable length for the field.  Parameter Name: Mail.
    At line:1 char:26
    + ... | foreach { Set-MsolUserPrincipalName -UserPrincipalName $_.UserPrinc ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [Set-MsolUserPrincipalName], MicrosoftOnlineException
        + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.StringLengthValidationException,Microsoft.Online.Administration.Automation.SetUserPrincipalName

    I can confirm the domain is verified, and that this is not a ADSync / On-Prem implementation, it's purely a cloud based approach.

    Thank You



    • Edited by JnrEnginnerNZ Tuesday, August 20, 2019 12:06 AM Code error
    Tuesday, August 20, 2019 12:04 AM

Answers

  • Please pay attention  to the error message:

     "Inputexceeded the maximum allowable length forthe field.  Parameter Name: Mail."

    Try it this way:

    Get-Msoluser -Department Test | 
        ForEach-Object {
            Try{
                $newUPN = '{0}.{1}@subdomain.domain.com' -f $_.firstname,$_.lastname
                Write-Host "$newUPN $($newUPN.Length)" -fore green
                Set-MsolUserPrincipalName -UserPrincipalName $_.UserPrincipalName -NewUserPrincipalName -ErrorAction Stop
            }
            Catch{
                Write-Host $_ -fore Red
            }
        }
    



    \_(ツ)_/

    • Marked as answer by JnrEnginnerNZ Tuesday, August 20, 2019 6:43 AM
    Tuesday, August 20, 2019 12:32 AM