# Remove Old smtp proxy addresses from 1000's of contacts

**URL:** <https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203>\
**Category:** PowerShell Help\
**Created:** [December 20, 2021, 9:20pm UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203 "2021-12-20T21:20:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![smithj](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@smithj](https://forums.powershell.org/u/smithj)\
**Post date:** [December 20, 2021, 9:20pm UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/1 "2021-12-20T21:20:12Z")

</div>

Hi All!

I’ve come across and issue where an email advisory addin to outlook is picking up on old smpt proxy addresses that are left over from our migration to exchange online. The app is supposed to warn users when they are emailing an external contact. We’ve discovered that this Addin is not longer recognising exchange contacts that still have an automatically created company smtp address.

I’ve kludged together a script that I’m having some difficulty with and any help would be appreciated:

```auto
$Users=Get-ADObject -SearchBase "OU=OU1,OU=OU2,OU=OU3,OU=OU4,DC=DC,DC=DC" `
-filter "proxyAddresses -like '*@Company*'" -properties proxyaddresses | Select DistinguishedName, @{name='smtp'; expression={($_.ProxyAddresses | Where-Object {$_ -like "*@Company*" })}}

Foreach ($User in $Users) 
{
Set-ADObject -Identity $user.DistinguishedName -Remove @{Proxyaddresses="$($user.smtp)"}
}

```

The Search part works fine but I can’t get the removal part to work. I think the problem lies with needing to use the Get-ADObject instead of the Get-ADUser the script was made for.

Any help is appreciated 🙂  
Jacob

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 20, 2021, 10:13pm UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/2 "2021-12-20T22:13:01Z")

</div>

Jacob,  
Welcome to the forum. 👋🏽

Before we proceed … when you post code or sample data or console output please format it as code using the _preformatted text_ button ( **\</\>** ). Simply place your cursor on an empty line, click the button and paste your code.  
Without that the forum software tries to interpret your code and may remove some characters like dollar signs. 😉  
Thanks in advance

> [@smithj](#):
>
> I can’t get the removal part to work.

That’s unfortunately not that helpful. What does that mean? Do you get error messages?

> [@smithj](#):
>
> I think the problem lies with needing to use the Get-ADObject instead of the Get-ADUser the script was made for.

Why do you think that? And why don’t you use `Get-ADUser` and `Set-ADUser`?

---

<div class="post-metadata">

**Author:** ![smithj](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@smithj](https://forums.powershell.org/u/smithj)\
**Post date:** [December 20, 2021, 10:31pm UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/3 "2021-12-20T22:31:50Z")

</div>

Hi Olaf Thanks for your reply.

I’m forced to use the Get-ADObject command because the objects I’m trying to edit are not users and didn’t show up when using the Get-ADUser command.

There is no error message when running the script and I think it may be due to using the DistinguishedName as the identifier for the contacts.

I can’t get the remove command to work when I do this manually:

```auto
Set-ADObject -Identity "DistinguishedName" -Remove @{Proxyaddresses=smtp:"fullproxyaddress"}

```

But when I use the ObjectGUID it will remove the Proxyaddress:

```auto
Set-ADObject -Identity ObjectGUID -Remove @{Proxyaddresses=smtp:"fullproxyaddress"}

```

Unfortunately this won’t accept the ObjectGUID as a property:

```auto
$Users=Get-ADObject -SearchBase "OU=OU1,OU=OU2,OU=OU3,OU=OU4,DC=DC,DC=DC" `
-filter "proxyAddresses -like '*@Company*'" -properties proxyaddresses | Select ObjectGUID, @{name='smtp'; expression={($_.ProxyAddresses | Where-Object {$_ -like "*@Company*" })}}

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 20, 2021, 11:39pm UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/4 "2021-12-20T23:39:21Z")

</div>

Hmmm … I just tried it and this worked for me:

```auto
$SearchBase = 'OU=OU1,OU=OU2,OU=OU3,OU=OU4,DC=DC,DC=DC'
$ContactList = Get-ADObject -LDAPFilter 'objectClass=Contact' -SearchBase $SearchBase -Properties ProxyAddresses |
    Where-Object { $_.ProxyAddresses -like '*@Company*' } |
        Select-Object *, @{name = 'smtp'; expression = { ($_.ProxyAddresses | Where-Object { $_ -like '*@Company*' }) } }
foreach ($Contact in $ContactList) {
    Set-ADObject -Identity $($Contact.DistinguishedName) -Remove @{ProxyAddresses = $($Contact.smtp) }
}

```

---

<div class="post-metadata">

**Author:** ![smithj](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@smithj](https://forums.powershell.org/u/smithj)\
**Post date:** [December 20, 2021, 11:51pm UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/5 "2021-12-20T23:51:24Z")

</div>

Cool! Thank you!  
I’ll give this a go now!

---

<div class="post-metadata">

**Author:** ![smithj](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@smithj](https://forums.powershell.org/u/smithj)\
**Post date:** [December 20, 2021, 11:56pm UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/6 "2021-12-20T23:56:02Z")

</div>

> [@Olaf](#):
>
> ```auto
> $SearchBase = 'OU=OU1,OU=OU2,OU=OU3,OU=OU4,DC=DC,DC=DC'
> $ContactList = Get-ADObject -LDAPFilter 'objectClass=Contact' -SearchBase $SearchBase -Properties ProxyAddresses |
> Where-Object { $_.ProxyAddresses -like '*@Company*' } |
> Select-Object *, @{name = 'smtp'; expression = { ($_.ProxyAddresses | Where-Object { $_ -like '*@Company*' }) } }
> foreach ($Contact in $ContactList) {
> Set-ADObject -Identity $($Contact.DistinguishedName) -Remove @{ProxyAddresses = $($Contact.smtp) }
> }
> 
> ```

I Get this error:

```auto
Set-ADObject : Invalid type 'System.Management.Automation.PSObject'.
Parameter name: ProxyAddresses
At line:6 char:5
+ Set-ADObject -Identity $($Contact.DistinguishedName) -Remove @{Pr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (CN=mgbs,OU=Exte...=Pharmac,DC=dom:ADObject) [Set-ADObject], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.SetADObject

```

Any Ideas?

---

<div class="post-metadata">

**Author:** ![smithj](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@smithj](https://forums.powershell.org/u/smithj)\
**Post date:** [December 21, 2021, 12:14am UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/7 "2021-12-21T00:14:56Z")

</div>

I Got it Sorted thank you!

There were multiple proxyaddresses that matched that search outcome.  
When I Specified the search parameters so that it would only do each one it worked!

Thank you SOO much for your help!

---

<div class="post-metadata">

**Author:** ![smithj](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@smithj](https://forums.powershell.org/u/smithj)\
**Post date:** [December 21, 2021, 12:30am UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/8 "2021-12-21T00:30:00Z")

</div>

So I do have one request if you are able to help.

Can you help me get this script running for multiple proxyaddresses per contact that fit the same search criteria?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 21, 2021, 3:47am UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/9 "2021-12-21T03:47:08Z")

</div>

Hmmm … very strange … it seems not to work at all when you provide more than one smtp address at a time. So you may remove them one by one. It’ll increase the runtime. But at least it does the job. 😉

```auto
$SearchBase = 'OU=OU1,OU=OU2,OU=OU3,OU=OU4,DC=DC,DC=DC'
$ContactList = 
    Get-ADObject -LDAPFilter 'objectClass=Contact' -SearchBase $SearchBase -Properties ProxyAddresses |
        Where-Object { $_.ProxyAddresses -like '*@Company*' } |
            Select-Object *, @{name = 'smtp'; expression = { ($_.ProxyAddresses | Where-Object { $_ -like '*@Company*' }) } }
foreach ($Contact in $ContactList) {
    foreach ($smtpAddress in $($Contact.smtp)) {
        Set-ADObject -Identity $($Contact.DistinguishedName) -Remove @{ProxyAddresses = $smtpAddress } 
    }
}

```

---

<div class="post-metadata">

**Author:** ![smithj](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@smithj](https://forums.powershell.org/u/smithj)\
**Post date:** [December 21, 2021, 7:06pm UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/10 "2021-12-21T19:06:44Z")

</div>

I managed to kludge together something that worked in a similar fashion:

```auto
$SearchBase = 'OU=OU,OU=OU,DC=DC,DC=dom'
$ContactList = Get-ADObject -LDAPFilter 'objectClass=Contact' -SearchBase $SearchBase -Properties ProxyAddresses |
    Where-Object { $_.ProxyAddresses -like '*@Company' } |
        Select-Object *, @{name = 'smtp'; expression = { ($_.ProxyAddresses | Where-Object { $_ -like '*@company' }) } }
foreach ($Contact in $ContactList) {
    foreach ($Search in $SearchBase){
        Set-ADObject -Identity $($Contact.DistinguishedName) -Remove @{ProxyAddresses = $($Contact.smtp) }
}}

```

Though it still balks at any name with a “-” in it.  
It left maybe 50 out of 2500 contacts. so that many I can edit manually.  
I really appreciate your help Olaf!  
Have a great Xmas!!!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:19pm UTC](https://forums.powershell.org/t/remove-old-smtp-proxy-addresses-from-1000s-of-contacts/18203/11 "2024-05-16T20:19:58Z")

</div>

Closed on May 16, 2024, 8:19pm UTC


