Shell – PowerShell – Remove specific SMTP addresses from proxyAddresses array

active-directorypowershell

I have been trying to figure this out how to modify the proxyAddresses of some of my users in AD using PowerShell. My basic goal is to use Get-ADObject to query AD for a specific set of users and remove all SMTP addresses for a specific domain from the user’s proxyAddresses (if it exists).

$ADobjects = @(Get-ADObject -Filter 'objectClass -eq "User"' -Properties mailNickname, ProxyAddresses) | Where-Object {$_.ProxyAddresses -Match "@BADdomain.com"}

ProxyAddresses might look like:

SMTP:john.doe@domainA.com, smtp:john.doe@BADdomain.com, smtp:john.doe@domainC.com, X500:/o=info/ou=test/cn=john.doe, x400:/o=info/ou=test/cn=john.doe, smtp:john.doe@domain.local

There is no specific order to this, X500 addresses may be first, may be last, SMTP, X400, etc. Uppercase SMTP indicates primary SMTP address, lowercase indicates secondary address. It is possible that john.doe@BADdomain.com may be the primary SMTP address (I will ignore case when splitting these – I can test for case later in the process to change the primary SMTP, if needed). And (I may be wrong) but it seems that proxyAddresses isn’t a normal array (maybe it is a multi-dimensional array)? But when I try to split the array on the “,”, I keep getting an error:

ForEach ($Object in $ADobjects)
{
$TempInfo = $Object.ProxyAddresses.Split(",")
write-host $TempInfo
}

The error that it produces is:

Method invocation failed because [Microsoft.ActiveDirectory.Management.ADPropertyValueCollection] doesn't contain a method named 'Split'.

Can I "split" proxyAddresses into a separate array so that I can work through each one and remove it if the domain name is @BADdomain.com? I could then remove it from the array. Or is it possible to use ".Replace" and replace everything starting with SMTP to BADdomain.com (if BADdomain.com is found prior to the next SMTP)? I don't know, I've tried may ways but have been unsuccessful.

Thanks much!!

Best Answer

Split is for splitting strings; the user object's ProxyAddresses property isn't a string, so it doesn't support "Split".

Try something like this (completely untested :) ):

ForEach ($userObject in $ADobjects)
{
  ForEach ($proxyAddress in $userObject.ProxyAddresses)
  {
    write-host $proxyAddress
  }
}
Related Question