Forum Discussion

StefanoC66's avatar
StefanoC66
Iron Contributor
Jun 28, 2024

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 domain to be removed, then remove it from the user's email addresses

 

How can I do that in powershell ?

thanks

 

  • Hi 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's avatar
    chrisslroth
    Brass Contributor
    Hi StefanoC66 check this:
    # Path to the CSV file containing the domains to be removed
    $domainsCsvPath = "C:\path\to\domains.csv"

    # Path to the CSV file containing user email addresses
    $usersCsvPath = "C:\path\to\users.csv"

    # Load the domains to be removed
    $domainsToRemove = Import-Csv -Path $domainsCsvPath | Select-Object -ExpandProperty domain

    # Load the user email addresses
    $users = Import-Csv -Path $usersCsvPath

    foreach ($user in $users) {
    # 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 ($domainsToRemove -contains $domain)
    }

    # Update the user's email addresses
    $user.EmailAddresses = ($filteredEmailAddresses -join ",")
    }

    # Export the updated user email addresses back to a CSV file
    $users | Export-Csv -Path "C:\path\to\updated_users.csv" -NoTypeInformation
    • StefanoC66's avatar
      StefanoC66
      Iron Contributor
      hello chrisslroth
      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 ?
      • chrisslroth's avatar
        chrisslroth
        Brass Contributor
        Hi 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)"
        }
        }

Resources