Best method to remove all emailaddresses except the primarysmtpaddress via EMS???
Hi All, I am working on removing all email addresses in the emailaddress field except for the primarysmtpaddress. This is being done to clean up the addresses that exist today. After much research and even more trial and error I found a couple of methods that work but can't help but think there must be a better way. If you have a script that is proven to accomplish this goal can you share it on this post? Below is what I have that works below. Method 1 uses a foreach statement and Method 2 uses the -emailaddresspolicyenabled property combined with a most likely null primarysmtpaddress value (I'm not quite sure what is going on under the covers here.) Can you also comment on why you would or wouldn't use one of these methods? Eventually, I have to get these commands into a .net program with code something like the following: Dim commands As New System.Collections.ObjectModel.Collection(Of System.Management.Automation.Runspaces.Command) Dim cmdSMAlias As New Command("Set-Mailbox") 'Create a command to set the mailbox alias 'Update the MailBox Alias field (Exchange updates the PrimarySmtpAddress when this field is changed) cmdSMAlias.Parameters.Clear() 'Clear out entries from the last student cmdSMAlias.Parameters.Add("Identity", upnBeingProcessed) cmdSMAlias.Parameters.Add("Alias", studentCommonName) 'Specified in the format lastName1234 commands.Add(cmdSMAlias) Finally, I'm not sure exactly how to add a statement like "get-mailbox -identity steststudent | %{$ea=$_.emailaddresses;$oa=$_.emailaddresses;foreach($a in $oa){if ($a.tostring() -ne ("SMTP:" + $_.primarysmtpaddress.tostring())){$ea -= $a}}$_ | set-mailbox -emailaddresses $ea}" into the .Net command structure above so if you know how this could be accomplished I would very much like to hear from you. Method 1: [PS] C:\>get-mailbox -identity steststudent | fl name, alias, primarysmtpaddress, emailaddresses Name : STestStudentAlias : STrialStudentPrimarySmtpAddress : STrialStudent@mstc.eduEmailAddresses : {SMTP:STrialStudent@mstc.edu} [PS] C:\>set-mailbox -identity steststudent -alias STestStudent[PS] C:\>get-mailbox -identity steststudent | fl name, alias, primarysmtpaddress, emailaddresses Name : STestStudentAlias : STestStudentPrimarySmtpAddress : STestStudent@mstc.eduEmailAddresses : {smtp:STrialStudent@mstc.edu, SMTP:STestStudent@mstc.edu} [PS] C:\>get-mailbox -identity steststudent | %{$ea=$_.emailaddresses;$oa=$_.emailaddresses;foreach($a in $oa){if ($a.tostring() -ne ("SMTP:" + $_.primarysmtpaddress.tostring())){$ea -= $a}}$_ | set-mailbox -emailaddresses $ea} The above code expanded: get-mailbox -identity steststudent | %{$ea=$_.emailaddresses;$oa=$_.emailaddresses;foreach($a in $oa){if ($a.tostring() -ne ("SMTP:" + $_.primarysmtpaddress.tostring())){$ea -= $a}}$_ | set-mailbox -emailaddresses $ea} [PS] C:\>get-mailbox -identity steststudent | fl name, alias, primarysmtpaddress, emailaddresses Name : STestStudentAlias : STestStudentPrimarySmtpAddress : STestStudent@mstc.eduEmailAddresses : {SMTP:STestStudent@mstc.edu} Method 2: [PS] C:\>get-mailbox -identity steststudent | fl name, alias, primarysmtpaddress, emailaddresses Name : STestStudentAlias : STrialStudentPrimarySmtpAddress : STrialStudent@mstc.eduEmailAddresses : {SMTP:STrialStudent@mstc.edu} [PS] C:\>set-mailbox -identity steststudent -alias STestStudent -emailaddresspolicyenabled:0 -emailaddresses $_.primarysmtpaddress[PS] C:\>get-mailbox -identity steststudent | fl name, alias, primarysmtpaddress, emailaddresses Name : STestStudentAlias : STestStudentPrimarySmtpAddress :EmailAddresses : {} [PS] C:\>set-mailbox -identity steststudent -emailaddresspolicyenabled:1[PS] C:\>get-mailbox -identity steststudent | fl name, alias, primarysmtpaddress, emailaddresses Name : STestStudentAlias : STestStudentPrimarySmtpAddress : STestStudent@mstc.eduEmailAddresses : {SMTP:STestStudent@mstc.edu}
March 23rd, 2010 12:00am

Seems like this would be a lot easier: $mbx = get-mailbox steststudent $newaddrs = $mbx.emailaddresses |? {$_ -cnotlike "smtp:*} set-mailbox $mbx -emaladdresses $newaddrs
Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2010 12:57am

mjolinor, Thanks for your reply. Your statement didn't quite match my criteria but it did stimulate some productive thought. This is what I came up with that initially works quite well: $mbx=get-mailbox steststudent$newaddrs = $mbx.emailaddresses|?{$_.IsPrimaryAddress}set-mailbox -identity steststudent -EmailAddresses $newaddrs Thank you for your post though as without it I wouldn't have thought about the where clause option. Steve
March 23rd, 2010 5:49pm

I'm glad you got it working, but i'm curious as to what it wasn't doing that you required. It should have removed all the secondary smtp addresses, leaving only the primary.
Free Windows Admin Tool Kit Click here and download it now
March 23rd, 2010 6:38pm

I'm probably doing something wrong as I'm easily baffled when it comes to powershell but this is what I get: [PS] C:\>get-mailbox steststudent | fl name, alias, primarysmtpaddress, emailaddresses Name : STestStudentAlias : STestStudentPrimarySmtpAddress : STestStudent@mstc.eduEmailAddresses : {smtp:strialstudent@mstc.edu, SMTP:STestStudent@mstc.edu} [PS] C:\>$mbx = get-mailbox steststudent[PS] C:\>$newaddrs = $mbx.emailaddresses |? {$_ -cnotlike "smtp:*}>>>>>>>> [Ctrl C to end][PS] C:\>$newaddrs | fl[PS] C:\> I couldn't get the statement to work because it went into >> mode. I also noticed that both addresses were prefixed by "smtp" so I didn't know if the statement would work the way that it was written. From there I started to play and found the following properties: [PS] C:\>$mbx.emailaddresses | fl SmtpAddress : strialstudent@mstc.eduAddressString : strialstudent@mstc.eduProxyAddressString : smtp:strialstudent@mstc.eduPrefix : SMTPIsPrimaryAddress : FalsePrefixString : smtp SmtpAddress : STestStudent@mstc.eduAddressString : STestStudent@mstc.eduProxyAddressString : SMTP:STestStudent@mstc.eduPrefix : SMTPIsPrimaryAddress : TruePrefixString : SMTP This uncovered the IsIsPrimaryAddress property which is what I ended up using. Thank you; I learned alot starting with your post.
March 26th, 2010 3:51pm

The >> was the console waiting to see of there were going to be any more commands. Hitting the enter key at >> tells it to go ahead and execute. This command: $newaddrs = $mbx.emailaddresses |? {$_ -cnotlike "smtp:*} Will remove all the secondary smtp addresses, and leave the primary smtp address and any other addresses (primary smtp address, x.500 address, etc) intact. As you have observed, in the emailaddresses collection the primary smtp address is prefixed SMTP:, and all the secondaries will be prefixed smtp:. -cnotlike is a case-sensetive operator. -cnotlike "smtp:*" will return anything that doesn't begin with a lower case "smpt:", so everything except the secondary smtp addresses will be returned.
Free Windows Admin Tool Kit Click here and download it now
March 26th, 2010 4:37pm

I just realized the problem you were having is because of an error in the script I gave you. It should have been this: $mbx = get-mailbox steststudent $newaddrs = $mbx.emailaddresses |? {$_ -cnotlike "smtp:*"} set-mailbox $mbx -emaladdresses $newaddrs The error was the missing closing quotes in the -cnotlike argument.
March 26th, 2010 4:54pm

No problems - I did look up the cnotlike after my last post. I can't believe I missed it the first time. Just curious is there a reason why you picked the cnotlike vs isprimaryaddress? I actually ended up with the following: $newaddrs = $mbx.emailaddresses|?{$_.IsPrimaryAddress -and $_.PrefixString -eq "SMTP"} Thanks again
Free Windows Admin Tool Kit Click here and download it now
March 26th, 2010 5:56pm

I picked that because I have other address types in my environment that would need to be preserved. You might or might not have those. If you don't then the primaryemailaddress solution would work fine. If you did have them, that solution could easily have unintended and unpleasant consequences.
March 26th, 2010 6:05pm

$newaddrs = $mbx.emailaddresses|?{$_.IsPrimaryAddress -and $_.PrefixString -eq "SMTP"} That'll work, but the -and $_.PrefixString -eq "SMTP" bit is kind of redundant. The primary smtp address cannot have any other prefix.
Free Windows Admin Tool Kit Click here and download it now
March 26th, 2010 6:07pm

The reason I stated it this way is because I didn't know if we could have a primary address that was not SMTP. It would probably be legacy if we did but as you mentioned I am acutely aware of the "unintended and unpleasant consequences" thing. Thanks again for your assistance.
April 12th, 2010 5:36pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics