Setting AD attributes from a CSV file

Back in this post I looked at setting the users home directory. I recently got a question about using a CSV file for input.

CSV files have been around for years and are likely to be with us for a long time to come – its a very useful and compact format.  using a CSV file to set attributes is a two stage process – read the data then make the changes.

I’ll use the home directory data as an example.  If you have a csv file with an entry for every user looking like this:

samaccountname : sjones
Drive          : H:
Home           :

You can then do this:

Import-Csv .\homes.csv |
foreach {
Set-ADUser -Identity $_.samaccountname -HomeDirectory $_.Home  -HomeDrive $_.Drive -PassThru

If the attribute doesn’t have a parameter then:

Import-Csv .\homes.csv |
foreach {
Set-ADUser -Identity $_.samaccountname -Replace @{HomeDirectory = “$($_.Home)”; HomeDrive = “$($_.Drive)”}  -PassThru

This entry was posted in PowerShell and Active Directory. Bookmark the permalink.

2 Responses to Setting AD attributes from a CSV file

  1. Del says:

    Thank You Richard!

    Very taken aback that you would spend your own time to write a whole article to my query!

    Testing this now! I assume the attributes (samaccountname …etc) can be listed in columns (A-C) as well as your example above of having them listed in rows.

    Great article and valuable for the community! Thank You again!

  2. Hem Thakur says:

    When i use above command It updates Profile, But does not create Folders in the File server. Kindly let me know what i need to do in order to get Folders Created Automatically on File server

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s