Forum Discussion
Modify users addresses via powershell
- 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)"
}
}
thanks for your help, just a note regarding the users that they're not extracted from a csv file but from a get-mailbox command so they need to be updated directly and not exported to a csv file.
Could you help with it ?
# 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)"
}
}
- StefanoC66Jul 01, 2024Iron ContributorIt seems that this test is not working as it doesn't filter any email address
# Filter out the email addresses that belong to the domains to be removed
$filteredEmailAddresses = $emailAddresses | Where-Object {
$domain = $_.Split("@")[-1]
-not ($domainsToRemove -contains $domain)
} - StefanoC66Jul 01, 2024Iron Contributor
It seems that there's some issue with the "domain filtering part"
to test the script I did the following
$NeedsNew = get-mailbox -identity "email address removed for privacy reasons" -ResultSize Unlimit
foreach ( $user in $NeedsNew ) {
# Split email addresses by comma (assuming they are stored in a comma-separated list in the 'EmailAddresses' field)
$emailAddresses = $user.EmailAddresses -split ","
# Filter out the email addresses that belong to the domains to be removed
$filteredEmailAddresses = $emailAddresses | Where-Object {
$domain = $_.Split("@")[-1]
-not ($Domtoremove -contains $domain)
}# Update the user's email addresses
$user.EmailAddresses = ($filteredEmailAddresses -join ",")but received the error
Exception setting "EmailAddresses": "Cannot convert value "smtp:email address removed for privacy reasons ....."
is not a valid SMTP address.""
At line:23 char:3
+ $user.EmailAddresses = ($filteredEmailAddresses -join ",")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting- chrisslrothJul 01, 2024Copper ContributorPlease 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- StefanoC66Jul 01, 2024Iron ContributorHi chrisslroth
Actually is failing setting back the email addresses
Set-Mailbox : Cannot convert 'ccci@xx.local,email address removed for privacy reasons' to the type 'Microsoft.Exchange.Data.ProxyAddressCollection' required by parameter
'EmailAddresses'. The address xx@xxx.local,email address removed for privacy reasons' is invalid: "xx@xxx.local,email address removed for privacy reasons" isn't a valid SMTP address. The domain name can't contain spaces and it has to have a prefix and a suffix, such as example.com.
At line:28 char:57
+ ... $mailbox.Identity -EmailAddresses ($filteredEmailAddresses -join ",")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Mailbox], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Exchange.Management.RecipientTasks.SetMailbox