1

I'm sure this is fairly basic but I can't seem to wrap my head around it and at this point it would almost be more prudent for me to do it manually but it seems like a good learning opportunity.

I created a custom attribute in AD. The users all have it set as an email to sync through a third party app, not associated with Active Directory. I need to change the domain name on all of them through Powershell from [email protected] to [email protected]. It seems easy enough but for some reason I can't get the Set-ADUser to change the property for each user and replace based off of the original username. I need to remove the last 27 characters of the old domain name, concatenate the current logon name with the new domain name, and then set it. It seems so simple yet I keep getting invalid arguments no matter how I try it in Powershell.

So far I have something like:

$Users = Get-ADUser -Filter "*" -SearchBase 'OU=test,DC=adDomain,DC=org' -Properties customattribute
ForEach-Object {
  $fullAttribute = Get-ADUser $_{customattribute}
  $logon -replace "fullAttribute.{27}$"
  $newLogon = $logon + "newdomainname.com"
  Set-ADUser $_ -replace @{customattribute=$newLogon}
}

I keep trying to take it in stages but I keep running into syntax errors.

2
  • 3
    I get that you are either trying to abstract or obfuscate your code, but it might help if you actually showed us the an example of an existing value and what it needed to be changed to.
    – Zoredache
    Aug 10, 2017 at 23:33
  • 2
    I also confused about what you think this line $fullAttribute = Get-ADUser $_{customattribute} is supposed to do. Where is the $logon variable being set? It isn't set to anything in your fragment.
    – Zoredache
    Aug 10, 2017 at 23:35

1 Answer 1

2

This should get you started. It is hard to test without the details @Zordache requested.

$olddomain ="billybob.com"
$newdomain = "newdomainname.com"

$Users = Get-ADUser -Filter "*" -SearchBase 'OU=test,DC=adDomain,DC=org' -Properties customattribute
ForEach-Object {
  $user = $_
  $fullAttribute = ($user.customattribute).tostring()
  $newattr = $fullAttribute.replace $olddomain $newdomain
  $user.customattribute=$newattr
  Set-ADUser -instance $user 
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .