Exchange SMTP Splitting at the "@"

I am trying to split our Primary SMTP addresses at the “@” so that I can set a secondary SMTP with a different ending after the “@”. I’ll keep out our actual domain. We currently have “” for our email addresses, but we want to add “” in perparation for a project. I have looked at a few options, but I am not a master scripter and I keep getting either errors, or results other than desired. Any help would be appreciated.

Import-Csv c:\temp\filename.csv | %{Get-Mailbox -Identity $_.Identity | ft PrimarySMTPAddress}

That is where I was trying to start from.


Well, stop using Format-Table. The output of that isn’t usable in code.

If you did something like…

$x = Import-Csv c:\temp\filename.csv | %{Get-Mailbox -Identity $_.Identity

Then $x would contain a bunch of mailbox objects. I would probably do this in a more script-based fashion, not a one-liner. So my second line of code would be to enumerate the mailboxes.

foreach ($box in $x) { }

In those {} is where you do all the work. You’re going to need to get the current address into a variable - say, $old. Then,

$pieces = $old -split '@'

After running that, $pieces[0] will contain whatever came before the @, and $pieces[1] will contain whatever came after the @. The @ itself will be gone. You can use those, hopefully, to construct your new email address.

Thank you, this is a good start.

This is where I’ve gotten to so far, but I can’t seem to get any output to verify that it is working.

$x=Get-Mailbox -Database ex\exsg2db
foreach ($box in $x) {$old=get-mailbox $_.Identity | Select PrimarySmtpAddress}
$pieces=$old -split ‘@’
Write-Host $new

So, you don’t need to do:

$old=get-mailbox $_.Identity | Select PrimarySmtpAddress

Because $box is already that mailbox. You also can’t use $_. And you need to put ALL the code inside the foreach loop.

$x=Get-Mailbox -Database ex\exsg2db
foreach ($box in $x) {
  $pieces = ($old.PrimarySmtpAddress) -split '@'
  $new = "$($pieces[0])"
  Write-Output $new          # we hates write-host

  # You could also just use
  # $new = $old.PrimarySmtpAddress -replace '',''
  # instead of everything else in currently the foreach loop

What you were doing before wasn’t working because you had it all outside the ForEach loop. Read “about_foreach” in the shell for more information on that construct; it is NOT the same as the ForEach-Object cmdlet and does not support $_.

Thank you so much. This has me started on the right path. I’m learning the scripting “under fire” and on my own. I appreciate all the help. I need to find some good resources to learn more before it gets too far past my abilities.

I’ll suggest my books :). and, then

It’s probably not necessary in this case, since Exchange should always be giving you well-formatted email addresses, but if you were accepting some string input that needs validation (and separation), I’d recommend just using the .NET System.Net.Mail.MailAddress class. If you try to cast a string to that type and it’s formatted wrong (multiple @ symbols, illegal characters, etc), you’ll get an exception. Once you have the object, it already provides “User” and “Host” properties you can read:

$maybeValidEmailAddresses = @('','')

foreach ($string in $maybeValidEmailAddresses) {
    try {
        $addr = [System.Net.Mail.MailAddress]$string
        Write-Host "User: $($addr.User), Host: $($addr.Host)"
    } catch {
        # Handle the error however you'd like.
        Write-Error -ErrorRecord $_