Categories

Setting AD attributes from a CSV file

Back in this post http://richardspowershellblog.wordpress.com/2012/02/29/setting-a-users-home-directory/ 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           :
\\server2\Home\sjones

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
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>