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 “@education.edu” for our email addresses, but we want to add “@ol.education.edu” 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}
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.
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])@ol.education.edu"
Write-Output $new # we hates write-host
# You could also just use
# $new = $old.PrimarySmtpAddress -replace 'education.edu','ol.education.edu'
# 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.
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: