Update AD from CSV

Suppose you have a CSV file (a text file with columns separated by commas) with the properties for AD user accounts you want to update. How do you do this in PowerShell?

Turns out, that we talked a lot about creating new accounts from CSV files before, but not about updating existing ones. Let’s fix this right away.

I will be using AD user accounts in my examples, but it is fairly easy to adapt them to other AD objects: groups, computers, OUs, DNS records, and so on.

The command actually depends on the CSV you get. The easiest case is when the column names are exactly the same as Set-QADUser parameters. For example, let’s say you have a CSV file in which you have a samAccountName column which you want to use to locate the accounts to update and Title and Department columns with the new values to set:

samAccountName,Title,Department
test1,Manager,Accounting
test2,Developer,RD
test3,SC,Pre-Sales

The onliner to apply this file to your AD is as simple as:

Import-Csv c:\update.csv | Set-QADUser -Identity { $_.samAccountName }

You basically pipe import into Set-QADUser and specify which column to use as the identity anchor.

Easy!

Now, suppose that life is not so easy and either you do not control the column labels or you need to update attributes which either do not match the parameter names or have no matching parameters at all. Like:

samAccountName,Job,ExtensionAttribute1,ExtensionAttribute2
test1,Manager,M,Yes
test2,Developer,S,No
test3,SC,XXL,Maybe

The automated column matching will not work here but we can use ForEach-Object loop and match the parameters manually + use ObjectAttributes for attributes with no parameters:

Import-Csv c:\update.csv | ForEach-Object {
Set-QADUser $_.samAccountName -Title $_.Job `
-ObjectAttributes @{ExtensionAttribute1=($_.ExtensionAttribute1);
ExtensionAttribute2
=($_.ExtensionAttribute2)}
}

Now we can update from CSV any account properties we want!

Tags: , , , , , , ,

About these ads

27 Responses to “Update AD from CSV”


  1. 1 Rich Harmer October 6, 2008 at 7:44 pm

    Can’t this be slightly modified to create users too?

  2. 2 Dmitry Sotnikov October 7, 2008 at 11:28 am

    Rich,

    Yes, you can use a similar approach with the New-QADUser cmdlet:

    http://dmitrysotnikov.wordpress.com/2008/01/21/ad-user-provisioning-from-csv-got-easier/

    Dmitry

  3. 3 Anuar June 25, 2009 at 1:02 pm

    How to deal with attribute that has dash ‘-’ in it?
    I got an error: “Missing ‘=’ operator after key in hash literal.”

  4. 4 Dmitry Sotnikov June 25, 2009 at 1:22 pm

    Anuar,

    Quotation marks should help. E.g.: @{ ‘attribute-with-dash’ = 25 }

    If I got the question wrong or you have more issues there please post your questions to our AD PowerShell forum at: http://powergui.org/forum.jspa?forumID=173

    Dmitry

  5. 5 Ozk4r February 3, 2011 at 1:24 am

    How i run this update from a schedule task?

    Thanks

  6. 8 Oscar February 9, 2011 at 12:23 am

    Excelent, Thank you!

  7. 9 Kirby June 21, 2011 at 6:25 pm

    If your cvs file just contains old phonenumbers and new phonenumbers can you use the old phonenumber for the identity and then replace it with the new phonenumber?

  8. 11 Kirby June 22, 2011 at 4:51 pm

    Hi Dmitry,
    Thanks for the help, it worked great. I added a small peice so I could run it agaist an targeted OU. Thanks again

    $OuDomain = “OU=The,OU=One,OU=You,OU=Want,DC=mydomain,DC=com”
    import-csv c:\scriptest\phonenumbers.csv | foreach-object { Get-QADUser -SearchRoot $OuDomain -PhoneNumber $_.OldNumber | Set-QADUser -PhoneNumber $_.NewNumber }

  9. 16 Vladimir Valchev September 11, 2011 at 10:21 pm

    Thank you Dmitry, saved my day

  10. 18 Michael November 14, 2011 at 3:03 pm

    I have used the Powershell Addin. It ist very usefull for me.
    But I have one problem. I have a csv file, but not every entry in the csv file have an user account. Everytime when the script not found the user, the script is stop the working.
    Could help me to solve this problem?

    Many thanks

    • 19 Dmitry Sotnikov November 14, 2011 at 3:22 pm

      Michael,

      One workaround would be to use Test-QADObject as described here: http://dmitrysotnikov.wordpress.com/2010/03/22/test-if-ad-object-path-exists/

      Dmitry

      • 20 Allan February 24, 2012 at 1:02 am

        Hi Dmitry,

        Am a novice Powershell user.

        Have tried to use your script and this does not appear to work for me.
        Not sure if it’s an environmental issue or something I am doing wrong.
        Also hard to see what’s happening as the script is not returning any errors – so it appears that this is working.

        Am trying to run the following script in a ps1 file: -

        Import-Csv “C:\Temp\update_users.csv” | Set-ADUser -server “inf003.org.local” -Identity { $_.samAccountName } 2>> “C:\Temp\User_Error_Log.txt”

        Script runs ok (as in no errors reported) however does not update the user in the CSV file.

        CSV file is as follows: -
        samAccountName,Title,EmployeeNumber
        user1,Testing123,567890

        So as you can see all CSV titles are correct and I think it should work?

        The changes I made to your original script at the top of this post was the server name (as reading other posts on the web I was coming up against a problem where I did not have sufficient rights to run the commands, even though I was logged in with DA privilages and running the console as Admin) and I am attempting to log any errors to a log file using 2>>

        Any thoughts?
        Help much appreciated!

      • 21 Dmitry Sotnikov February 24, 2012 at 3:20 am

        Allan,

        I don’t work at the team anymore so I would really recommend that you contact them via AD and PowerShell forum at http://powergui.org

        My guess is that direct pipeline from Import-CSV to Set-QADUser is broken and you should put foreach in-between:

        Import-Csv “C:\Temp\update_users.csv” | ForEach { Set-ADUser -Identity $_.samAccountName -Title $_.Title -ObjectAttributes @{EmployeeNumber=$_.EmployeeNumber} }

        Dmitry

        P.S. I love Alan Parson’s Project. ;)

  11. 22 Allan February 24, 2012 at 5:01 am

    Thanks mate!
    Have followed your advice and posted on the AD and PowerShell forum at http://powergui.org

    Allan

    • 23 Anonymous July 24, 2012 at 3:49 pm

      Hi Dmitry

      I tried your script Import-Csv c:\update.csv | Set-QADUser -Identity { $_.sAMAccountName } but it return argument invalid for Identity what is wrong with me

      Nathalie

  12. 25 Joe April 4, 2013 at 2:14 pm

    Worked great, Thanks


  1. 1 Skip empty CSV fields « Dmitry’s PowerBlog: PowerShell and beyond Trackback on October 7, 2008 at 8:00 am
  2. 2 Blogs for Technological Boniks - Update the information of domain users by CLI or Powershell Trackback on July 12, 2012 at 6:47 am

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s




My Recent Tweets

RSS My company’s blog

  • Fun at HostingCon 2013
    The smell of the hosting industry’s finest is in the air, and of course we sent some of the Jelastic team to attend and proudly exhibit at HostingCon 2013 this week in Austin, Texas.  From what I have been reading, there are more than 50 scheduled sessions from Sales and Marketing, Technology, Issues and Trends and [...]The post Fun at HostingCon 2013 appear […]
  • Secure Access to Your Jetty Web Application
    Today’s post focuses on the web application security related features of Jetty app server. After reading this article you should be able to configure security realms to provide authentication and access control for your Jetty web application, as well as to grant access to your app for dedicated IP-addresses only. Before we start let’s examine what Jetty real […]
  • Software Stacks Market Share: May 2013
    We are back to update you with the latest trends in software stacks popularity for May 2013. This time we decided to collect the numbers in a different way to get more accurate statistics. As you remember previously we counted the number of the environments, where each software stack was used. We’ve changed the reporting [...]The post Software Stacks Market […]
  • Setting Up a Cronjob in Jelastic Cloud
    Cronjob allows you to configure regularly scheduled tasks so that the jobs can be run automatically at a pre-set point of time. It repeats itself and does not need any regular manual instructions. Cron automates your system and can be used for quite different purposes. This wonderful tool is a standard part of all sysadmins toolkit. Also cronjob has a [...]T […]
  • How to Deploy Joget Cluster into the Cloud
    Joget Workflow is an open source platform that allows you to build enterprise web applications easily, due to its rich set of tools. It is also a rapid application development platform that provides complete agile development capabilities, including consisting of processes, forms, lists, CRUD and UI; not just back-end EAI/orchestration/integration or the tas […]
  • Liferay Cluster in the Cloud
    Liferay Portal is one of the most popular Java CMSs in the world due to its impressive ease-of-use. Since we published the tutorial on Liferay deployment to the cloud we have seen an extremely positive reaction from its community. Also we have received multiple requests from Liferay fans asking about clustering, replication and fail-over capabilities in the […]
  • Mark Zbikowski Veteran Microsoft Architect Joins Jelastic
    Jelastic already has an impressive team of advisers including Serguei Beloussov, the founder of Parallels. We also have technical geniuses who endorse and use Jelastic including the father of Java James Gosling, David Blevins who founded the TomEE project, and Michael “Monty” Widenius, the author of the original version of the open-source MySQL database and […]

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my former employer - Quest Software, or my current employer - Jelastic or anyone else for that matter. All trademarks acknowledged.

© 2007-2013 Dmitry Sotnikov

Pages

October 2008
M T W T F S S
« Sep   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Follow

Get every new post delivered to your Inbox.

Join 67 other followers

%d bloggers like this: