SOLVED

Script to populate AD fields

%3CLINGO-SUB%20id%3D%22lingo-sub-3214703%22%20slang%3D%22en-US%22%3EScript%20to%20populate%20AD%20fields%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3214703%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI%20have%20a%20bunch%20of%20active%20user%20accounts%20where%20only%20the%20username%20field%20has%20been%20populated.%26nbsp%3B%20I%20need%20to%20populate%20the%20First%20Name%2C%20Last%20Name%20and%20E-Mail%20fields.%3C%2FP%3E%3CP%3ECan%20anyone%20help%20me%20with%20a%20script%20I%20could%20use%20which%20could%20find%20the%20username%20in%20a%20csv%20file%20and%20add%20the%20missing%20fields%20in%20AD%3F%3C%2FP%3E%3CP%3EI'm%20thinking%20csv%20file%20would%20contain%20username%2Cfirstname%2Clastname%2Cemail%20%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3214703%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EActive%20Directory%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3214952%22%20slang%3D%22en-US%22%3ERe%3A%20Script%20to%20populate%20AD%20fields%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3214952%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1318118%22%20target%3D%22_blank%22%3E%40BuckMacD%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20have%20a%20.csv%20file%20with%20these%20lines%20in%20them%20(only%20added%20as%20example%2C%20you%20can%20add%20as%20much%20as%20you%20want%20of%20course)%2C%20you%20can%20use%20the%20script%20below%20to%20put%20values%20in%20Active%20Directory%20users%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eusername%2Cfirstname%2Clastname%2Cemail%3C%2FP%3E%3CP%3Etestuser1%2Ctest%2Cuser1%2CEmail%20address%20removed%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eforeach%20(%24user%20in%20Import-Csv%20C%3A%5CScripts%5Cusers.csv)%20%7B%0ASet-ADUser%20-Identity%20%24user.username%20-GivenName%20%24user.firstname%20-Surname%20%24user.lastname%20-EmailAddress%20%24user.email%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20you%20can%20add%20more%20in%20your%20csv%20of%20course%20and%20use%20the%20other%20paramters%20in%20Set-Aduser%20(%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Factivedirectory%2Fset-aduser%3Fview%3Dwindowsserver2022-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Factivedirectory%2Fset-aduser%3Fview%3Dwindowsserver2022-ps%3C%2FA%3E)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3215018%22%20slang%3D%22en-US%22%3ERe%3A%20Script%20to%20populate%20AD%20fields%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3215018%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20a%20lot%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1209009%22%20target%3D%22_blank%22%3E%40Harm_Veenstra%3C%2FA%3E.%20I'll%20give%20this%20a%20shot%20on%20Monday.%20%3Athumbs_up%3A%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3215036%22%20slang%3D%22en-US%22%3ERe%3A%20Script%20to%20populate%20AD%20fields%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3215036%22%20slang%3D%22en-US%22%3ENo%20problem%2C%20let%20us%20know%20if%20it%20worked%20out%20for%20you%20and%20have%20a%20good%20weekend!%3C%2FLINGO-BODY%3E
New 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 (New 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!