Script for converting Bounce Back LegacyExchangeDN to the X500 format

Scenario:  You have to convert the LegacyExchangeDN that is being provided to you in bounce back emails with the special charter formatting into an X500 address.  This may need to be done if mailboxes are disabled and recreated as new mailboxes or when performing migrations.  

From the bounce back message, copy and paste the address into the $Addr variable below and then execute this script:


$Addr = "IMCEAEX-_O=TEST+20ENTERPRISE+20EXCHANGE_OU=EXCHANGE+20ADMINISTRATIVE+20GROUP+20+28FYDIBOHF23SPDLT+29_CN=RECIPIENTS_CN=4bd8a35a90e2441a4587635898d62f9f-s@domain.com"

$Repl= @(@("_","/"), @("\+20"," "), @("\+28","("), @("\+29",")"), @("\+2C",","), @("\+5F", "_" ), @("\+40", "@" ), @("\+2E", "." ))

$Repl | ForEach { $Addr= $Addr -replace $_[0], $_[1] }

$Addr= "X500:$Addr" -replace "IMCEAEX-","" -replace "@.*$", ""

Write-Host $Addr