Share via


Script to modify a custom attribute for bulk AD users

Question

Friday, October 14, 2016 7:51 PM

Hello,

I'm new to scripting and need to pull from an attribute and place in a new custom attribute.   Specifically I'd like to to pull from Property Name: mail and title E-mail, copy to a custom attribute  mail2 and replace the @domain with a secondary @domain.  I need to do this for users in a specific OU.  

I've tried look for examples and have come close.  Can I do this in one script? Do I need to first copy the attribute and then do a replace or can I do this in one command?

Thanks

All replies (9)

Tuesday, October 18, 2016 6:17 PM ✅Answered

Get-ADUser -Filter { Surname -eq 'Doe' } -SearchBase 'OU=Users,DC=domain,DC=net' -Properties EmailAddress, warriorEMail |
    ForEach-Object{
        $_ | Set-ADObject -Replace @{ warriorEmail = ($_.EmailAddress -replace  '@domain1.com', '@newdomain.com') } 
    }

You should not use surname.  You should use loginID (samaccountname) like this:

$samname = 'jsmith'
Get-ADUser $samname -Properties EmailAddress, warriorEMail |
    ForEach-Object{
        $_ | Set-ADObject -Replace @{ warriorEmail = ($_.EmailAddress -replace  '@domain1.com', '@newdomain.com') } 
    }

\(ツ)_/


Tuesday, October 18, 2016 8:49 PM ✅Answered

Thanks this one liner worked for me.  I'm using surname just for my test, I plan to change the filter to all users in a specific OU.

Get-ADUser -Filter {Surname -eq"Doe"} -SearchBase "OU=Users,DC=Domain,DC=com" -Properties EmailAddress, warriorEmail | Select-Object * -First 2 | ForEach-Object {Set-ADObject -Identity $_.DistinguishedName -Replace @{warriorEmail = ($_.EmailAddress -replace  '@domain1.com', '@domain2.com')} }

In summary this script will populate a custom attribute that was created "warriorEmail" using the "Mail" attribute and replacing the domain portion of the email address with a different domain Domain2.com.   

This was needed so that we can use G Suite Password Sync (GSPS) tool.  Their tool won't sync AD passwords with G Suite if the "Mail" attribute has a different domain than G Suite.  So by creating a new Attribute, we can configure GSPS to look at "warriorEmail" and the email address will match G suite and push AD passwords to G suite.

Thanks for you help in writing the script. 


Friday, October 14, 2016 8:28 PM

You have start by learning how to write a script.  Also use the PowerShell help system and research the Active Directory CmdLets.

New scripters must always start by learning the basics.  You cannot learn the basics by asking questions.

Here is the resource for AD PowerShell: https://technet.microsoft.com/en-us/library/ee617195.aspx

Here is the learning page: https://technet.microsoft.com/en-us/dd742419

Here is the script Gallery with pre-written scripts: https://gallery.technet.microsoft.com/

Review the material and write yor script.  Post back with specific questions about any issues you are having.

\(ツ)_/


Friday, October 14, 2016 8:38 PM

LOL thanks for the links...durrrrrrrrrr


Monday, October 17, 2016 6:02 AM

Hi derismano,

It will be useful if you could post your current scripts here, for further assistances.

Besides, I suppose you could start from below codes:

Get-ADUser -Filter *  -Properties *  | select -Property mail | foreach {$_ -replace "oldmail","newmail"}

Best regards,

Andy

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact [email protected].


Tuesday, October 18, 2016 4:29 PM

Thanks for reply, I accomplished the first part of the script which copies the EmailAddress string from the Mail attribute to a custom attribute "warriorEmail".

Get-ADUser -Filter {Surname -eq"Doe"} -SearchBase "OU=Users,DC=domain,DC=net" -Properties EmailAddress, warriorEmail | Select-Object * -First 2 | ForEach-Object {Set-ADObject -Identity $_.DistinguishedName -Replace @{warriorEmail=$($_.EmailAddress)}}

So now in my custom attribute I have [email protected] that we populated from the EmailAddress string. 

Now I would like to replace part of the string in the new custom attribute warriorEmail with a different domain.  i.e. replace @domain.com with @newdomain.com.  

Thanks


Tuesday, October 18, 2016 4:37 PM

$newmail = $_.warriorEmail - replace <'@domain1.com','@newdomain.com'>

\(ツ)_/


Tuesday, October 18, 2016 5:33 PM

Thanks for quick reply JRV.  Can you give me an example of where I would add or input that line? 


Tuesday, October 18, 2016 8:50 PM

You are welcome.  Good luck.

\(ツ)_/