PowerShell Script Add-EmailSuffix.ps1: Bulk Add Email Address Suffix in Exchange 2010

I recently had a need to bulk add email addresses to accounts in bulk.  I didn’t want to use Email Address Policies (for some non-related reason) so I decided to put together a handy PoSH script to add the addresses.

The Script has a few cool features:

  • Verifies the email address suffix is valid (including non-publicly routable addresses, i.e. domain.local).
  • Accepts input from the pipeline so you can filter the results of Get-Mailbox to quickly add the addresses.
  • Validates Email address has been added.
  • Output to screen.
  • Included help section.

Below is the source, or you can download Add-EmailSuffix.ps1


<#  
.SYNOPSIS  
   	Adds email address suffix to mailbox based on Alias.
.DESCRIPTION  
    This script is intended to enable bulk addition of email addresses.
.NOTES  
    Version 			: 1.0 - 06/28/2012 - initial release
	Rights Required		: Recipient Admin in Exchange Org
    Exchange Version	: 2010
    Author       		: Robert Durkin
    Author Email		: rdurkin@iccohio.com
	Author Blog			: http://ehloworld.net
    Disclaimer   		: You running this script means you won't blame me if this breaks your stuff.

.LINK  
	http://ehloworld.net/?p=250
.PARAMETER Mailboxes
    The Identity parameter specifies the mailbox.
    This parameter accepts the following values:
    * Example: JPhilips
    * Example: Atlanta.Corp.Contoso.Com/Users/JPhilips
    * Example: Jeff Philips
    * Example: CN=JPhilips,CN=Users,DC=Atlanta,DC=Corp,DC=contoso,DC=com
    * Example: Atlanta\JPhilips
    * Example: fb456636-fe7d-4d58-9d15-5af57d0354c2
    * Example: fb456636-fe7d-4d58-9d15-5af57d0354c2@contoso.com
    * Example: /o=Contoso/ou=AdministrativeGroup/cn=Recipients/cn=JPhilips
    * Example: Jeff.Philips@contoso.com
    * Example: JPhilips@contoso.com

.PARAMETER EmailSuffix
	Valid email address suffix in the format of '@domain.suffix'

.EXAMPLE
	Get-Mailbox | .\Add-EmailSuffix.ps1 -EmailSuffix "@domain.com"
	This would add the email address suffix @ohio.gov to all mailboxes.		

.EXAMPLE
	.\Add-EmailSuffix.ps1 -Mailboxes "bob.durkin@domain.com" -EmailSuffix "@domain.com"
	Add the email address suffix @domain.com to the mailbox with bob.durkin@domain.com.

.INPUTS
	Accepts pipline input for the Mailboxes parameter.
.OUTPUTS
	To Screen
#>

#Requires -Version 2.0

##############
# Parameters #
##############

param([parameter(Mandatory=$True, ValueFromPipeline=$true, Position=0)][Object[]]$Mailboxes,
	  [parameter(Mandatory=$True, ValueFromPipeline=$false)][String]$EmailSuffix)

########
# Main #
########

begin {
	#Regex to test email address.  Does not check for internet routable compliance, by design.
	$EmailAddressRegex = "@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
}

process {

	#Test for valid email address suffix format.
	If (!($EmailSuffix -match $EmailAddressRegex)) {
		Write-Host "Invalid Email Address Suffix. Please specify a valid address suffix (i.e. @domain.com)." -ForegroundColor Red
		Exit
	}
	
	Foreach ($Mbx in $Mailboxes) {
		[string]$NewAddress = $Mbx.Alias+$EmailSuffix
		Write-Host "Adding $NewAddress to" $Mbx.Alias
		Set-Mailbox $Mbx -EmailAddresses @{Add=$NewAddress}
		If ((Get-Mailbox $Mbx).EmailAddresses | ?{$_.SmtpAddress -like "*$EmailSuffix"}) {
			Write-Host "Verified that $NewAddress is a valid email address on $Mbx.Alias" -ForegroundColor Green
		} Else {
			Write-Host "Failed to add $NewAddress to $Mbx.Alias" -ForegroundColor Red
		}
	}
}
Posted in Exchange 2010, PowerShell

4 comments on “PowerShell Script Add-EmailSuffix.ps1: Bulk Add Email Address Suffix in Exchange 2010

  • Attempting to modify the script to use:

    $EmailAddressRegex = “@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?”

    Still did not work. I will continue to experiement until I hear back. Thanks!

    • Hi Ryan,

      I’m glad you found this script useful. For your purposes you could remove the validation that the Regex matching designed to check for. That would include commenting out line 70 through 74. Give that a try.

      Cheers!

  • Hey Robert! Thanks for the script as it seems that it would be very useful for my purposes as well. I am in the same boat in that using email address policies is NOT an option because of the complexity of our alias assignment.

    I am attempting to use the script to add @domain.mail.onmicrosoft.com for all of our on-premise mailboxes so that they can take advantage of our O365 archiving licensing. I assume their is something about $EmailAddressRegex that needs to be adjusted to accomidate for the additional dots in my alias. I say this because the scipt is never able to validate this domain. Any advice on this is welcomed and appreciated! Keep up the great contributions.

    • Hi Ryan,

      Yes, I can see why that would fail. The regex was only designed to handle a suffix with up to 3 domain name parts. I have to be honest, I’m not the best with regex. There is a better way to do this I’m sure. I’ll see if I can figure something out.

      Thanks for the feedback!

Leave a Reply