Script to Delete Proxy Addresses from AD

DAAQIN 26 Reputation points
2022-03-29T18:38:41.567+00:00

need a script to delete proxy addresses from AD.

The script needs to delete only a specific proxy address from each user.
john.smith.mx360@Company portal .com

I have the script to get the information, unfortunately, I was only able to figure out how to output it to a text file, but is hard to read. In any case, I got the info I need and have the email addresses and I've into a spreadsheet and saved it to C:\Tem\proxiestoremove.csv. I could not figure out how to do this as a batch but only one by one. Note am in a hybrid organization and needs to be done from AD as Exchange won't work.

There are 83 emails to remove and they are not all from the same user.

82 users = 83 emails to remove (1 from each user)

Import-Module ActiveDirectory  
$User = Get-ADUser john.smith -Properties proxyAddresses  
$User.proxyAddresses.Remove("smtp:john.smith.mx360@contoso.com")  
Set-ADUser -instance $User  

Thanks

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,846 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
{count} vote

Accepted answer
  1. Newbie Jones 1,306 Reputation points
    2022-03-30T14:16:03.147+00:00

    This might work. (The first part works, but I haven't tested the remove command).
    Just in case you have an account with more than one mx360 address.

    Get-ADUser -Properties proxyaddresses -Filter {ProxyAddresses -like '*mx360@contoso.com'} |
        ForEach { # Account may have more than one email address in scope so need to loop through each one
            ForEach ($proxyAddress in $_.proxyAddresses) {
                If ($proxyAddress -like '*mx360@contoso.com') {
                    # Write-Host $proxyAddress
                    Set-ADUser $_.SamAccountName -Remove @{ProxyAddresses=$proxyAddress}                    
                }
            }      
        }
    
    2 people found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2022-03-29T19:23:56.777+00:00

    You can try this (I haven't run this code!):

    $User = Get-ADUser ole.roemer -Properties proxyaddresses
    $OldProxies = $User | Select-Object -expand proxyaddresses
    $newproxies = @()
    $OldProxies |
        ForEach-Object{
            if ($_ -notmatch "\.mx360@"){
                $newproxies += $_
            }
        }
    $User | Set-ADUser -Replace @{proxyaddresses = $newproxies}
    

  2. Newbie Jones 1,306 Reputation points
    2022-03-30T13:17:26.477+00:00

    Some food for thought.

    For your list of users, you can filter directly (server side) on the proxy address.

    Get-ADUser -Properties proxyaddresses -Filter {ProxyAddresses -like '*mx360@Company portal .com'}

    The problem with this is that the proxyAddresses is a collection of strings and you can't directly output this for the next command (unless you are confident is only going to return one address per account).

    If you are confident that an account can only have one mx360 email address, then in theory, you should be able to pipe the result of the filter directly into your ForEach loop with the remove command.

    SamAccountName and proxyAddresses will be part of the results and can be used directly. ($.SamAccountName\$.proxyAddresses)
    No need for select-object for any other client side filtering.

    The problem is if the account has more than one mx360 email addresses, which you would then need to deal with and make the script a bit more complex.

    0 comments No comments

  3. DAAQIN 26 Reputation points
    2022-03-30T16:37:36.133+00:00

    I send up fixing this on my own two scripts worked for this:

        $Users = Get-ADUser -Filter * -pr proxyaddresses
        foreach ($User in $Users) {
            $User.proxyaddresses | Where-Object { ($_ -like "*mx360*") } |
            ForEach-Object {
                Get-ADuser $user.DistinguishedName | Set-ADuser  -remove @{proxyaddresses = $_ } -WhatIf
                Write-Host "Removing "$_" from " $User.name "'s Proxy address" -ForegroundColor Green
            }
        }
    

    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

    $users = Get-ADUser -Filter * -pr proxyaddresses
    foreach($User in $Users){
        Write-host $user
        Foreach($proxyaddress in $user.proxyaddresses){
            If ($proxyaddress -like "*mx360*"){
                write-host $proxyaddress
                get-aduser -identity $user.distinguishedname -pr proxyaddresses | Set-ADUser -remove @{proxyaddresses = $proxyaddress} -WhatIf
            }
        }
    }
    

  4. Abfalter, Simon 0 Reputation points
    2023-03-22T17:54:23.9333333+00:00

    I used a wrong email address policy and added a wrong domain to all users.

    To fix my mistake I applied the correct policy so that all users have set the correct primary smtp address. Than I let run this script to remove the wrong domain on all users that dont't have the domain as Userprincipalname:

    $Users = Get-ADUser -properties proxyAddresses -filter {proxyAddresses -like "*@wrong_address_added_by_mistake.com" -and UserPrincipalName -notlike "*@wrong_address_added_by_mistake.com"}
    
    foreach ($User in $Users) {
        $Remove = $User.proxyAddresses | Where-Object {$_ -like "*@wrong_address_added_by_mistake.com"}
        $User.proxyAddresses.Remove($Remove)
        Set-ADUser -Instance $User
    }