remove secondary email address of a lists of users with powershell
Hi guys, trying to remove a list of users from different domain that have secondary email address contain the word _site@domain.com is that possible with powershell? Thanks
June 24th, 2010 11:07am

Hi Zom , You can try this...I adapted it from a script that's running here. It's taking only the accounts that are enabled and shown in the address book. Change the file path and you'll have in a txt file the user mailbox that changed and the address removed, if there's a glitch! :) [object]$private:oQADUser = $null [object]$private:oQADUsers = $null [object]$private:oUserMailBox = $null [object]$private:oUserMailBoxes = New-Object System.Collections.ArrayList(0) [string]$private:strEmailAddress = "" [object]$private:oEmailAddresses = New-Object System.Collections.ArrayList(0) [string]$private:strPrimarySMTP = "" [string]$private:strAddressList = "" [string]$private:strFileContent = "" [string]$private:strFileOutput = "D:\PowerShell\Prod\Temp\Set-Mailbox.Remove.EmailAddresses.002-.txt" $oQADUsers = Get-QADUser -SizeLimit 0 -Enabled:$true -IncludedProperties msExchMailboxGuid, showInAddressBook | Where-Object { (($_.msExchMailboxGuid -ne $null) -and ($_.showInAddressBook -ne $null)) } $oQADUsers | Where-Object {$_ -ne $null } | ForEach-Object { $oQADUser = $_ $oUserMailBox = Get-Mailbox -resultsize unlimited –identity $oQADUser.DN $oEmailAddresses = $oUserMailBox.EmailAddresses $strAddressList = "" $strPrimarySMTP = $oUserMailBox.PrimarySmtpAddress.ToString() $oEmailAddresses | Where-Object {$_ -ne $null} | ForEach-Object { $strEmailAddress = $_.proxyaddressstring.tostring() if ($strEmailAddress -ne $strPrimarySMTP) { $strAddressList += $strEmailAddress + "," [void]$oEmailAddresses.Remove($_) } } # End $oEmailAddresses | ForEach-Object Set-Mailbox -Identity $oUserMailbox -EmailAddresses $oEmailAddresses if ($strAddressList -ne "") { $strFileContent = "$oUserMailBox, $strAddressList" $strFileContent >> $strFileOutput } } # End $oQADUsers | ForEach-Object Regards. Shafaquat Ali. M.C.I.T.P Exchange 2007 M.C.I.T.P Windows Server 2008 M.C.T.S OCS Server 2007 R2
Free Windows Admin Tool Kit Click here and download it now
June 24th, 2010 12:04pm

Hi guys, trying to remove a list of users from different domain that have secondary email address contain the word "_site" is that possible with powershell? Thanks From your post title it seems that u want to remove email addresses, while your post description says that you want to remove a list of users. Can u clear it? If you want to remove a list of users from a specific domain whose any secondary email address contains the word "_site", then here is the script for this task. Write-Host "Enter domain name:" $dom = [Console]::ReadLine() $dom = $dom.Trim() $mbxes = get-mailbox |where {$_.PrimarySmtpAddress -like '*@'+$dom} foreach($mbx in $mbxes){ Write-Host "Processing mailbox: $($mbx.DisplayName) ($($mbx.PrimarySmtpAddress))" $bDeleteMailbox = $false for ($i=0;$i -lt $mbx.EmailAddresses.Count; $i++) { $address = $mbx.EmailAddresses[$i] if ($address.IsPrimaryAddress -eq $false) { if($address.AddressString.ToString().Contains("_site")) { write-host "`nSecondary Email Address( $($address.AddressString) ) Matched" -f "magenta" $bDeleteMailbox=$true break; } } } if($bDeleteMailbox) { write-host "`nRemoving mailbox $($mbx.PrimarySmtpAddress)" Remove-mailbox $mbx } } Before removing the mailbox this script will confirm it by asking Yes or No, which u can remove by adding -Confir:$false with Remove-mailbox like this Remove-mailbox $mbx -Confirm:$false Regards, Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
June 24th, 2010 2:49pm

thanks for the reply guys, well sorry that not being clear enough, i will explain the scenario here. We will need to remove the secondary email address with a csv lists of users which their email address contain _site@domain.com and only their secondary email address will be remove but not their mailbox as they are still using the primary smtp address. Possible we import the csv lists of users to the script and remove the secondary email addrress, hope that i m clear enough and sorry for the confusion at first. Thank you.
Free Windows Admin Tool Kit Click here and download it now
June 25th, 2010 6:35am

any advice and help guys? Thanks
June 26th, 2010 3:47am

Hi Zom , Here is the script just update it as per your need and get the results as per your needs. Get-Content C:\users.txt |Get-Mailbox | foreach { for ($i = $_ .EmailAddresses.Count ;$i -ge 0; $i -- ) { $_ .EmailAddresses [$i ].ProxyAddressString if ($_ .EmailAddresses [$i ].ProxyAddressString -like "smtp:*" ) { $_ .EmailAddresses.RemoveAt ($i ) } } $_ |set-mailbox } Attached script will remove all the secondary email address for the given set of users Regards. Shafaquat Ali. M.C.I.T.P Exchange 2007 M.C.I.T.P Windows Server 2008 M.C.T.S OCS Server 2007 R2
Free Windows Admin Tool Kit Click here and download it now
June 26th, 2010 10:39am

Hi Zom , Here is the script just update it as per your need and get the results as per your needs. Get-Content C:\users.txt |Get-Mailbox | foreach { for ($i = $_ .EmailAddresses.Count ;$i -ge 0; $i -- ) { $_ .EmailAddresses [$i ].ProxyAddressString if ($_ .EmailAddresses [$i ].ProxyAddressString -like "smtp:*" ) { $_ .EmailAddresses.RemoveAt ($i ) } } $_ |set-mailbox } The script is incorrect dear, have u tested it? Also there is space between $_ and . so script wont compile, so remove them before testing. First mistake is that it is for removing all sec email addresses, while O.P requested to remove a specific email address. 2. Problem is that this script gives errors Exception calling "RemoveAt" with "1" argument(s): "Cannot remove the primary SMTP address which shows that above check if ($_ .EmailAddresses [$i ].ProxyAddressString -like "smtp:*" ) is incorrect to find only secondry email addresses. I modified it to some extent, but it is still removing all email addresses, which I am explorying, why, which is related to the index changing at runtime, when you remove an email address from the email addresses collection $_ .EmailAddresses. I have tested it this scripts runs but removes all email addresses: Get-Content C:\users.txt |Get-Mailbox | foreach {  write-host "$($_.Name) ---- $($_.EmailAddresses.Count)" for ($i = $_.EmailAddresses.Count ;$i -ge 0; $i -- ) {  $_.EmailAddresses[$i].ProxyAddressString    if ($_.EmailAddresses[$i].IsPrimaryAddress  -eq $false )  { $_.EmailAddresses.RemoveAt($i) }   }    set-mailbox  $_.Identity -EmailAddresses $_.EmailAdresses } I have a solution, and will test it and post here. Regards,Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
June 26th, 2010 2:50pm

Hi, This script is running on my Ex 2007 SP2 perfectly. Get-Content C:\users.txt |Get-Mailbox | foreach{ for ($i=0;$i -lt $_.EmailAddresses.Count; $i++) { $address = $_.EmailAddresses[$i] if ($address.IsPrimaryAddress -eq $false -and $address.SmtpAddress -like "*_site*" ) { Write-host("Remove smtp adress: " + $address.AddressString.ToString() ) $_.EmailAddresses.RemoveAt($i) } }         Set-Mailbox -Instance $_ } Regards, Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM) www.HostingController.com
Free Windows Admin Tool Kit Click here and download it now
June 26th, 2010 3:19pm

hi guys, thanks for the help will test it out and keep u guys posted. Thank you.
June 28th, 2010 8:49am

tested on the scripts and it works like a charm! thanks alot for the help guys. btw if i would just let the script run through all the mailbox rather than from a list can i change the script to this? and also export the result to csv. Get-Mailbox -result Unlimited | foreach{ for ($i=0;$i -lt $_.EmailAddresses.Count; $i++) { $address = $_.EmailAddresses[$i] if ($address.IsPrimaryAddress -eq $false -and $address.SmtpAddress -like "*_site*" ) { Export-csv c:\export.csv | Write-host("Remove smtp adress: " + $address.AddressString.ToString() ) $_.EmailAddresses.RemoveAt($i) } } Set-Mailbox -Instance $_ } Thank you guys!
Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2010 6:31am

Got it to write to file, thanks. Get-Mailbox -result Unlimited | foreach{ for ($i=0;$i -lt $_.EmailAddresses.Count; $i++) { $address = $_.EmailAddresses[$i] if ($address.IsPrimaryAddress -eq $false -and $address.SmtpAddress -like "*_site*" ) { Write-host("Remove smtp adress: " + $address.AddressString.ToString() | out-file c:\test.txt -append ) $_.EmailAddresses.RemoveAt($i) } } Set-Mailbox -Instance $_ }
July 5th, 2010 6:22am

beautiful script!!!!
Free Windows Admin Tool Kit Click here and download it now
October 27th, 2010 5:35pm

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

Other recent topics Other recent topics