SOLVED

Script to populate AD fields

Copper Contributor

Hi,

I have a bunch of active user accounts where only the username field has been populated.  I need to populate the First Name, Last Name and E-Mail fields.

Can anyone help me with a script I could use which could find the username in a csv file and add the missing fields in AD?

I'm thinking csv file would contain username,firstname,lastname,email ?

4 Replies
best response confirmed by BuckMacD (Copper Contributor)
Solution

@BuckMacD 

 

If you have a .csv file with these lines in them (only added as example, you can add as much as you want of course), you can use the script below to put values in Active Directory users: 

 

username,firstname,lastname,email

testuser1,test,user1,Emailaddress

 

 

 

 

foreach ($user in Import-Csv C:\Scripts\users.csv -Delimiter ',') {
Set-ADUser -Identity $user.username -GivenName $user.firstname -Surname $user.lastname -EmailAddress $user.email
}

 

 

 

 

But you can add more in your csv of course and use the other parameters in Set-Aduser (https://docs.microsoft.com/en-us/powershell/module/activedirectory/set-aduser?view=windowsserver2022...)

Thanks a lot @Harm_Veenstra. I'll give this a shot on Monday. :thumbs_up:

No problem, let us know if it worked out for you and have a good weekend!

Thanks @Harm_Veenstra 

That seems to be working well.  Tried it out on a small subset of test users and it does what it should.

I'm going to request a bit more user info from our MIS team and use that to populate some more useful fields in AD (description, department etc)

I'm still a bit of a noob with Powershell but didn't expect a simple one-liner to do everything I needed!

Cheers for your help!

1 best response

Accepted Solutions
best response confirmed by BuckMacD (Copper Contributor)
Solution

@BuckMacD 

 

If you have a .csv file with these lines in them (only added as example, you can add as much as you want of course), you can use the script below to put values in Active Directory users: 

 

username,firstname,lastname,email

testuser1,test,user1,Emailaddress

 

 

 

 

foreach ($user in Import-Csv C:\Scripts\users.csv -Delimiter ',') {
Set-ADUser -Identity $user.username -GivenName $user.firstname -Surname $user.lastname -EmailAddress $user.email
}

 

 

 

 

But you can add more in your csv of course and use the other parameters in Set-Aduser (https://docs.microsoft.com/en-us/powershell/module/activedirectory/set-aduser?view=windowsserver2022...)

View solution in original post