locked
Update AD From CSV by Attribute Value RRS feed

  • Question

  • We are attempting to update our AD fields from a database that holds all our users phone numbers.  The only accurate attribute between AD and the CSV is the EmployeeNumber.  I am having a tough time wrapping my head around how this should be scripted to tie the two together by this field and update phone numbers (and possibly more info) for each user.

    Any tips would be appreciated.  

    Wednesday, October 8, 2014 1:51 PM

Answers

  • Hi,

    Assuming your CSV headers are EmployeeNumber and PhoneNumber, try this:

    Import-Csv .\employeeList.csv | ForEach {
    
        $user = Get-ADUser -Filter "EmployeeNumber -eq '$($_.EmployeeNumber)'"
        
        Set-ADUser -Identity $user -OfficePhone $_.PhoneNumber -WhatIf
    
    }

    Remove -WhatIf if you're happy with the initial output to actually make the changes.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Proposed as answer by Richard MuellerMVP Wednesday, October 8, 2014 2:26 PM
    • Marked as answer by USAF2007 Wednesday, October 8, 2014 2:48 PM
    Wednesday, October 8, 2014 2:04 PM

All replies

  • Hi,

    Assuming your CSV headers are EmployeeNumber and PhoneNumber, try this:

    Import-Csv .\employeeList.csv | ForEach {
    
        $user = Get-ADUser -Filter "EmployeeNumber -eq '$($_.EmployeeNumber)'"
        
        Set-ADUser -Identity $user -OfficePhone $_.PhoneNumber -WhatIf
    
    }

    Remove -WhatIf if you're happy with the initial output to actually make the changes.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    • Proposed as answer by Richard MuellerMVP Wednesday, October 8, 2014 2:26 PM
    • Marked as answer by USAF2007 Wednesday, October 8, 2014 2:48 PM
    Wednesday, October 8, 2014 2:04 PM
  • There are two similar attributes in AD, employeeNumber and employeeID, so make sure you refer to the right one for your situation.

    Richard Mueller - MVP Directory Services

    Wednesday, October 8, 2014 2:30 PM
  • Works like a champ - thanks Mike and Richard!
    • Edited by USAF2007 Wednesday, October 8, 2014 2:58 PM
    Wednesday, October 8, 2014 2:48 PM
  • Wednesday, October 8, 2014 4:23 PM
  • Okay another request....script works great as I said but now I need to exclude a OU in AD. I know I can use .DistinguishedName -like "OU=ouname, DC=DomainName, DC=org"  or some variation.  I am having a hard time understanding how to combine everything to get it to work.

    Thanks!

    Thursday, November 6, 2014 6:28 PM
  • Okay another request....script works great as I said but now I need to exclude a OU in AD. I know I can use .DistinguishedName -like "OU=ouname, DC=DomainName, DC=org"  or some variation.  I am having a hard time understanding how to combine everything to get it to work.

    Thanks!

    This topic has been answered and closed. Please start a new topic.  I also recommend spending some time in the schoolroom here:

    http://technet.microsoft.com/en-us/scriptcenter/dd793613


    ¯\_(ツ)_/¯

    Thursday, November 6, 2014 6:32 PM