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 4: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 5:04am
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
June 24th, 2010 5:04am
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
Free Windows Admin Tool Kit Click here and download it now
June 24th, 2010 7:49am
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 7:49am
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 24th, 2010 11:35pm
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.
June 24th, 2010 11:35pm
any advice and help guys? Thanks
Free Windows Admin Tool Kit Click here and download it now
June 25th, 2010 8:47pm
any advice and help guys? Thanks
June 25th, 2010 8:47pm
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 3: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 7:50am
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
Free Windows Admin Tool Kit Click here and download it now
June 26th, 2010 7:50am
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
June 26th, 2010 8:19am
hi guys, thanks for the help will test it out and keep u guys posted. Thank you.
Free Windows Admin Tool Kit Click here and download it now
June 28th, 2010 1:49am
hi guys, thanks for the help will test it out and keep u guys posted. Thank you.
June 28th, 2010 1: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 1st, 2010 11:31pm
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!
July 1st, 2010 11:31pm
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 $_
}
Free Windows Admin Tool Kit Click here and download it now
July 4th, 2010 11:22pm
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 4th, 2010 11:22pm
beautiful script!!!!
Free Windows Admin Tool Kit Click here and download it now
October 27th, 2010 5:29pm
beautiful script!!!!
October 27th, 2010 5:29pm
Would any changes be needed in order to run this with Exchange 2010 SP1? I have copied/pasted the script, but get the error below. The only changes I have made are the import file and the specific smtp string to search on. The "Write-host" portion does return
the user name as well as the proper email address to remove.
A positional parameter cannot be found that accepts argument '<<User name>>'.
+ CategoryInfo : InvalidArgument: (:) [Set-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Set-Mailbox
Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2012 3:55pm
Would any changes be needed in order to run this with Exchange 2010 SP1? I have copied/pasted the script, but get the error below. The only changes I have made are the import file and the specific smtp string to search on. The "Write-host" portion does return
the user name as well as the proper email address to remove.
A positional parameter cannot be found that accepts argument '<<User name>>'.
+ CategoryInfo : InvalidArgument: (:) [Set-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Set-Mailbox
May 23rd, 2012 4:04pm
Would any changes be needed in order to run this with Exchange 2010 SP1? I have copied/pasted the script, but get the error below. The only changes I have made are the import file and the specific smtp string to search on. The "Write-host" portion does return
the user name as well as the proper email address to remove.
A positional parameter cannot be found that accepts argument '<<User name>>'.
+ CategoryInfo : InvalidArgument: (:) [Set-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Set-Mailbox
It seems that set-mailbox is failing.
You can try to replace this line
Set-Mailbox -Instance $_
with this one
Set-Mailbox -Identity $_.Identity -EmailAddresses $_.EmailAddresses
or with
Set-Mailbox -Identity $_ -EmailAddresses $_.EmailAddresses
Regards,
Laeeq Qazi|Team Lead(Exchange + Sharepoint + BES + DynamicsCRM)
www.HostingController.com
Free Windows Admin Tool Kit Click here and download it now
May 24th, 2012 4:05am
Thanks!
Using the "Set-Mailbox -Identity $_ -EmailAddresses $_.EmailAddresses" worked fine.
May 30th, 2012 10:23am
Thanks!
Using the "Set-Mailbox -Identity $_ -EmailAddresses $_.EmailAddresses" worked fine.
Free Windows Admin Tool Kit Click here and download it now
May 30th, 2012 10:26am