Powershell to ADD, Remove and Extract Email Address

Hi Readers,

We recently had a requirement to remove old decommissioned domain email address from user mailboxes,

so We have taken a cautious approach & written three scripts, that can be downloaded from below link.

https://gallery.technet.microsoft.com/scriptcenter/Powershell-to-ADD-Remove-85adf04b

ReportEmailAddress  –> Reporting the user mailboxes that have old email address

RemoveEmailAddress –> Get input from the ReportEmailAddress

AddemailAddress –> For Rollback purposes

Open Extract_emlrep.ps1 inside Reportemailaddress:

Define the emailaddress that is required to be removed & run Extractemlrep.bat

$email = “*@labtest.com”

report will be extracted in the form of a csv file.

DisplayName Alias SmtpAddress EmailAddresses

 

Copy this csv file to RemoveEmailAddress

Now execute the Removeemailaddress.ps1 “Name of Csv file”

This will remove the email address mentioned in smtpAddress from the user mailboxes.

If you want to rollback than copy the csv file in AddemailAddress folder & execute the script similar toRemoveemailaddress.ps1

Pls rate the script if it works for you 🙂

#################################################################################### 
#             Author: Alex Rodrick 
#                       Reviewer: Vikas Sukhija 
#             Date:- 01/23/2015 
#           Description:- This script will extract particular emails addresses 
#################################################################################### 
###########################Define variables######################################### 
 
 
$email = "*@labtest.com" 
 
$days = (get-date).adddays(-60) 
$date = get-date -format d 
$date = $date.ToString().Replace(“/”, “-”) 
$time = get-date -format t 
$month = get-date  
$month1 = $month.month 
$year1 = $month.year 
$time = $time.ToString().Replace(":""-"$time = $time.ToString().Replace(" """) 
 
$output1 = ".\" + "ExtractemailRep_" + $date + "_" + $time + "_.csv" 
 
# Add Exchange Shell... 
 
If ((Get-PSSnapin | where {$_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010"}) -eq $null) 
{ 
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 
} 
 
 
 
$a = get-mailbox -resultsize unlimited  
 
$b = $a | select DisplayName,@{Name="EmailAddresses";Expression={$_.emailaddresses}},Alias -expand EmailAddresses | where {$_.smtpAddress -like "$email"| Select DisplayName , Alias , smtpAddress,EmailAddresses 
 
$b  | Export-csv $output1 -notypeinformation  
 
#################################################################################### 
#################################################################################### 
#             Author: Alex Rodrick 
#                       Reviewer: Vikas Sukhija 
#             Date:- 01/23/2015 
#           Description:- This script will extract particular emails addresses 
#################################################################################### 
###########################Define variables######################################### 
 
# Add Exchange Shell... 
 
If ((Get-PSSnapin | where {$_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010"}) -eq $null) 
{ 
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 
} 
 
$z = import-csv $args[0] 
$count = $z.count 
write-host " Total Count of rows to process is $count" -foregroundcolor green 
 
for ($i=0; $i -le $count-1 ; $i++) 
  { 
   $alias = $z[$i].alias 
   Write-host "Processing ................. $alias" -foregroundcolor green 
   $x =  $alias | Get-Mailbox 
   $email = $z[$i].SmtpAddress 
   $c = $x.EmailAddresses | where {$_.smtpAddress -like "$email"} 
   Set-Mailbox $x -EmailAddresses @{remove=$c} 
  } 
 
#################################################################################### 
#################################################################################### 
#             Author: Alex Rodrick 
#                       Reviewer: Vikas Sukhija 
#             Date:- 01/23/2015 
#           Description:- This script will add email address to users for reverting 
#           the process done by remove email address script 
#################################################################################### 
###########################Define variables######################################### 
# Add Exchange Shell... 
 
If ((Get-PSSnapin | where {$_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010"}) -eq $null) 
{ 
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 
} 
 
 
$z = import-csv $args[0] 
$count = $z.count 
write-host " Total Count of rows to process is $count" -foregroundcolor green 
 
for ($i=0; $i -le $count-1 ; $i++) 
  {  
   $alias = $z[$i].alias 
   Write-host "Processing ................. $alias" -foregroundcolor green 
   $x =  $z[$i].alias 
   $s =  $z[$i].SmtpAddress 
   Set-Mailbox $x -EmailAddresses @{add=$s} 
  } 
 
#####################################################################################

Regards

Sukhija Vikas

http://msexchange.me

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s