Wednesday, February 11, 2015

Change Exchange primary email address to lower case

By default Microsoft Exchange creates email addresses on the form First.Last@domain.com. If you change the email address policy to use lowercase, the existing email addresses are not updated to reflect that change.

It doesn't work to change the primary SMTP address to lower case right away, since email addresses are basically case insensitive. When you set a new primary SMTP address, the old address is kept as an alias email address. Exchange can't set a new primary email address since the same address already exists as an alias. More or less, you get the picture.

I created the following script to make the change


# Get all mailboxes in the users OU (select the OU for which you want to change to lower case
$Mailboxes = Get-Mailbox -OrganizationalUnit "OU=Users,DC=domain,DC=com" -ResultSize Unlimited
# Loop through all mailboxes
ForEach ($Mailbox in $Mailboxes)
{
    # Turn off email address policy
    Set-Mailbox -Identity $Mailbox.Identity -EmailAddressPolicyEnabled $false
    # Set a temporary primary email address
    Set-Mailbox -Identity $Mailbox.Identity -PrimarySmtpAddress "dummy@domain.com"
    # Remove the old primary address which is now a standard email address connected to the mailbox
    Set-Mailbox -Identity $Mailbox.Identity -EmailAddresses @{Remove=$Mailbox.PrimarySmtpAddress}
    # Convert the old primary address to lowercase
    $lowercasesmtp = $Mailbox.PrimarySmtpAddress.ToLower()
    # Set the lowercase email address as primary
    Set-Mailbox -Identity $Mailbox.Identity -PrimarySmtpAddress $lowercasesmtp
    # Remove the temp primary address
    Set-Mailbox -Identity $Mailbox.Identity -EmailAddresses @{Remove='dummy@domain.com'}
    # Turn on the address policy
    Set-Mailbox -Identity $Mailbox.Identity -EmailAddressPolicyEnabled $true
    # Check the result
    Get-Mailbox -Identity $Mailbox.Identity | select Name, *SMTP*, Email*
}

5 comments:

  1. amazing script :) works flawless
    one question here, what if we want to change the case from user@domain.com to user@Domain.com
    to be specific user@xy.com to user@xY.com

    ReplyDelete
    Replies
    1. Hi,

      This changed script changes the email address to user@Domain.com. Be careful as I haven't tested it on a live Exchange system though.

      # Get all mailboxes in the users OU (select the OU for which you want to change to lower case
      $Mailboxes = Get-Mailbox -OrganizationalUnit "OU=Users,DC=domain,DC=com" -ResultSize Unlimited
      # Loop through all mailboxes
      ForEach ($Mailbox in $Mailboxes)
      {
      # Turn off email address policy
      Set-Mailbox -Identity $Mailbox.Identity -EmailAddressPolicyEnabled $false
      # Set a temporary primary email address
      Set-Mailbox -Identity $Mailbox.Identity -PrimarySmtpAddress "dummy@domain.com"
      # Remove the old primary address which is now a standard email address connected to the mailbox
      Set-Mailbox -Identity $Mailbox.Identity -EmailAddresses @{Remove=$Mailbox.PrimarySmtpAddress}

      # Changed to make the email address as user.name@Domain.com
      # Convert the old primary address to lowercase
      $lowercasesmtp = $Mailbox.PrimarySmtpAddress.ToString().ToLower()
      # Find the position of the @ in the string
      $at_position = $lowercasesmtp.IndexOf("@")
      # The part of the email address to the left of the @
      $email_user = $lowercasesmtp.Substring(0,$at_position)
      # The first part character to the right of @ as uppercase, and keep the rest in lowercase
      $email_domain = $lowercasesmtp.Substring($at_position+1,1).ToUpper() + $lowercasesmtp.Substring($at_position+2,$lowercasesmtp.Length-$at_position-2)
      # Set the variable $lowercasesmtp to the new format. Never mind the variable name, it should probably be renamed, but I keep it so we don't need to change the rest of the script.
      $lowercasesmtp = $email_user + "@" + $email_domain
      # END OF CHANGES

      # Set the lowercase email address as primary
      Set-Mailbox -Identity $Mailbox.Identity -PrimarySmtpAddress $lowercasesmtp
      # Remove the temp primary address
      Set-Mailbox -Identity $Mailbox.Identity -EmailAddresses @{Remove='dummy@domain.com'}
      # Turn on the address policy
      Set-Mailbox -Identity $Mailbox.Identity -EmailAddressPolicyEnabled $true
      # Check the result
      Get-Mailbox -Identity $Mailbox.Identity | select Name, *SMTP*, Email*
      }

      Delete
  2. The syntax in the first example is giving me errors under Windows Server 2012 R2, on the lowercase conversion. The syntax in second example works:

    $lowercasesmtp = $Mailbox.PrimarySmtpAddress.ToString().ToLower()

    ReplyDelete
  3. I'm getting following error; any help is really appreciated.

    Cannot process argument transformation on parameter 'EmailAddresses'. Cannot convert value
    "System.Collections.Hashtable" to type "Microsoft.Exchange.Data.ProxyAddressCollection". Error: "Conversion from
    Microsoft.Exchange.Data.SmtpAddress to Microsoft.Exchange.Data.ProxyAddress has not been implemented."
    + CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
    + PSComputerName : au02bp01v01n.unisyswps.local

    ReplyDelete
    Replies
    1. I added Set-Mailbox -Identity $Mailbox.Identity -EmailAddresses @{Remove=$Mailbox.PrimarySmtpAddress.Address} to fix the problem. PrimarySMTPAddress was returning multiple values, "Address" was one among them

      Delete