Monthly Archive

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

Leave a Reply