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 = ""

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

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

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

Write-Host $Addr