Remove a String from a Multi-Valued Attribute


Happy to Help

This week a friend asked for a little extra-vocational assistance. A multi-valued attribute in Active Directory, populated on for a large number of users objects, contained an entry in a non-standard format. To make things interesting, whilst the non-standard format was the same for each user, the information contained within each entry was different. To make things even more interesting, there were a number of legitimate entries that needed maintaining.

 

More Context 

A multi-valued attribute, as the name suggests, can contain multiple values. The one in question was proxyAddresses. It can contain a number of strings representing stuff to do with Microsoft Exchange.

 

The Challenge

Find all users that have a proxyAddresses entries starting with ":" and then remove the whole string. Ensure the code works with v2 of PowerShell.

 

The Solution

$users = Get-ADUser -Filter {ProxyAddresses -like ":*"} -Properties ProxyAddresses

if ($users) {

foreach ($user in $users) {

$OddProxies = $user.ProxyAddresses | where-object {$_ -like ":*"}

foreach ($OddProxy in $OddProxies) {

Set-ADUser -Identity $user -Remove @{ProxyAddresses = "$($OddProxy)"}

}

}

}


An Explanation

First search for any users that have a proxy address entry starting with ":".

Request the proxyAddresses property and store the results in $users.

If $users is populated, loop through each user object found. Each user object iteration will be represented as $user.

For each user identify the non-standard formatting by using a where object filter on ":". The whole string of each user's non-standard entry is then stored in the $OddProxies array. We create an array to allow for the fact that more than one entry might be found.

Now, loop through the $OddProxies array and run Set-ADUser for each non-standard entry. $OddProxy is the current iteration in the loop. The -Remove parameter is used to remove anything that matches the current $OddProxy iteration. The other entries are maintained.

 

Comments (4)

  1. nybrett8 says:

    This is great! Thank you

  2. nybrett8 says:

    This is great! Thank you

  3. Erik N says:

    This is very nice script, but {ProxyAddresses -like “:*”} should be {ProxyAddresses -like “*:*”}

Skip to main content