Office 365 Bulk UPN Update RRS feed

  • Question

  • Hello,

    I have a list of users in a CSV, has current UPN of "", 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 As you can see, the UPN has to be changed to, rather than

    The 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 -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 + '')

    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


  • 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 {
                $newUPN = '{0}.{1}' -f $_.firstname,$_.lastname
                Write-Host "$newUPN $($newUPN.Length)" -fore green
                Set-MsolUserPrincipalName -UserPrincipalName $_.UserPrincipalName -NewUserPrincipalName -ErrorAction Stop
                Write-Host $_ -fore Red


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