attribute removal

Hi there,

I have a script which we made to remove an erroneous proxy address that was initially made in error (a typo)

import-module activedirectory
$cred = Get-Credential

$users = get-aduser -searchbase "OU=Users,OU=example,OU=campus,DC=domain,DC=internal" -filter{pager -eq "Staff" -and enabled -ne $false -and proxyaddresses -like "*"} -properties displayname, UserPrincipalName, proxyaddresses, mailnickname, mail #| select displayname,  UserPrincipalName, proxyaddresses, mailnickname, mail

foreach ($user in $users)

$name = $user.SamAccountName
$UPN = $user.userprincipalname
$removeproxy = 'smtp:'+ $name + '*' -replace " ","." 
# $removeproxy2 = 'smtp:'+ $UPN
# $replaceproxy = 'smtp:'+ $user.displayname + '' -replace " ","." 
# $newproxy = 'SMTP:'+ $UPN
# $newmail = $UPN
# $student = 'smtp' + $name + ''

if ($user.proxyaddresses -eq $removeproxy) 

{ write-output "proxyaddress present for $name changing data" | out-file "c:\Success_change_proxy_data.txt" -append

set-aduser -identity $name  -remove @{proxyaddresses=$removeproxy} -WhatIf
#set-aduser -identity $name -add @{proxyaddresses=$replaceproxy,$newproxy;mail=$newmail}

{write-output "Proxyaddress not present for $name no change" | out-file "c:\Error_change_proxy_data.txt" -append}

At the moment, it searches for people with the provided searchbase and removes an smtp address which matches the above query.

We have another smtp address which we would like to remove, however, due to these members of staff having had namechanges in the past. Their SamAccountName no longer matches their proxy address.

It might all sound confusing, but all i really need to do, is edit the $removeproxy variable, to accept a wildcard, e.g

‘smtp’ +’*

I know this is simple, but im having a very bad mental block at the moment!

Could you just put it into an IfThen loop to check for the wild-carded info and then perform an action on match? Like this:

$name = 'will.anderson'

$removeproxy = 'smtp:'+ $name + '*' -replace " ","."

If ($removeproxy -like ('smtp:'+ $name + "*")){

    Write-Output "Stuff Happened."


Hi Will,

Thank you for your reply,

I’m searching quite a big set of users, although narrowed down to staff, trouble is i don’t necessarily know their name, so i would rather it just loop through the OU i’ve specified. Which it does currently do, but i’d just like to wildcard the variable, rather than re-write it, if that makes sense?

It might be the lack of coffee, but I’m not sure I’m comprehending. Let me see if I’ve got this right. You’re

  1. Targeting a specific OU for users.
  2. Looking to see if they have a property that contains ‘
  3. Change that property value to a wildcard address?


Sorry Will,

The opposite for me, too much caffeine now my brain is mush!

So, currently, it searches for ‘smtp’ + $name + ‘

Because of the way married / divorced staff accounts are held, their SamAccountName (where $name takes its value from) Isn’t the same as the smtp address that we are trying to remove (in their old name), thus that particular code wont’t quite work.

What i would like that line to do is something more like this, ‘smtp’ + ‘*

Basically, everything within the OU that i currently have in the searchbase, but without specifying the $name variable, i want ALL of them.

Hope that makes a bit more sense!