Forum Discussion
StefanoC66
Jun 28, 2024Iron Contributor
Modify users addresses via powershell
I need to make the following: - from a list of domains to be removed from email addresses in a csv file load them. - for each user compare all his email addresses and if anyone is from the list of ...
- Jul 01, 2024Hi StefanoC66 please check this:
# Path to the CSV file containing the domains to be removed
$domainsCsvPath = "C:\path\to\domains.csv"
# Load the domains to be removed
$domainsToRemove = Import-Csv -Path $domainsCsvPath | Select-Object -ExpandProperty domain
# Get all mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited
foreach ($mailbox in $mailboxes) {
# Get all email addresses for the mailbox
$emailAddresses = $mailbox.EmailAddresses | Where-Object { $_ -like 'SMTP:*' } | ForEach-Object { $_.SmtpAddress }
# Filter out the email addresses that belong to the domains to be removed
$filteredEmailAddresses = $emailAddresses | Where-Object {
$domain = $_.Split("@")[-1]
-not ($domainsToRemove -contains $domain)
}
# Update the mailbox with the filtered email addresses
if ($filteredEmailAddresses.Count -ne $emailAddresses.Count) {
Set-Mailbox -Identity $mailbox.Identity -EmailAddresses ($filteredEmailAddresses -join ",")
Write-Host "Updated email addresses for mailbox: $($mailbox.Identity)"
}
}
chrisslroth
Jul 01, 2024Brass Contributor
Please check your csv-file.
# Load the domains to be removed
$domainsToRemove = Import-Csv -Path $domainsCsvPath | Select-Object -ExpandProperty domain
-> the property-name in the first line in csv-file must be domain, to import the correct values for the mailaddresses
# Load the domains to be removed
$domainsToRemove = Import-Csv -Path $domainsCsvPath | Select-Object -ExpandProperty domain
-> the property-name in the first line in csv-file must be domain, to import the correct values for the mailaddresses
StefanoC66
Jul 01, 2024Iron Contributor
Actually my domains csv file is like
DomainA,DomainB
xxx.com,yy.com
Because I'm using the same file to filter domains from two separate ORG.
and I loaded the domains to be removed as the following
$Domto remove = Import-Csv -Path $Domainfile | select DomainA | where {$_.DomainA -ne ""}
DomainA,DomainB
xxx.com,yy.com
Because I'm using the same file to filter domains from two separate ORG.
and I loaded the domains to be removed as the following
$Domto remove = Import-Csv -Path $Domainfile | select DomainA | where {$_.DomainA -ne ""}