Update AD attributes from a csv file

Naveen Kumar 21 Reputation points
2022-05-07T13:46:51.813+00:00

I need to update all below mentioned AD attributes from a CSV file, please let me know if i can update these in one script or do i need multiple scripts? Please help me with detailed steps, i am not so good in scripting. Thanks.

CSV file have filled:
Samaccountname,emailaddress,officephone,manager,Title,EmployeeID,Office,Department,Company

emailaddress
officephone
manager
Title
EmployeeID
Office
Department
Company

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,215 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,455 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,831 Reputation points
    2022-05-08T02:03:58.807+00:00

    You could continue adding the individual parameters and values to the Set-ADUser cmdlet, making it longer and more cumbersome to deal with, of you could use a hash to assemble the parameters and values and use "splatting" to keep the clutter down.

    I also added some minimal error handling.

    Import-Csv -Path "C:\temp\adinfo.csv" | 
        ForEach-Object {
            # properties from the csv
            $acct = $_.samaccountname     # needed for error message
            $props = @{
                EmployeeID   = $_.EmployeeID
                Office       = $_.Office
                title        = $_.title
                Department   = $_.Department
                emailaddress = $_.emailaddress
                officephone  = $_.officephone
                manager      = $_.manager
                company      = $_.company
    
            }
            Try {
                Get-ADUser -Identity $_.SamAccountName -Properties * -ErrorAction STOP| 
                    Set-ADUser @props -ErrorAction STOP
                }
                Catch {
                    Write-Host "User '$acct' not found or failed to update: "
                    Write-Host $_
                }
            }
    

    EDIT: Added $<Underbar> to original $acct = $samaccountname # needed for error message

    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,831 Reputation points
    2022-05-07T14:48:54.003+00:00

    Every one of those attributes have a corresponding parameter on the Set-ADUser cmdlet. You can do what you need in a few lines of code.


  2. Andreas Baumgarten 103.8K Reputation points MVP
    2022-05-07T15:53:38.82+00:00

    Hi @Naveen Kumar ,

    for the Set-AdUser command you will find the parameters and examples here: https://learn.microsoft.com/en-us/powershell/module/activedirectory/set-aduser?view=windowsserver2022-ps

    Here you will find some examples how to work with Set-AdUser and how to update multiple users using a csv file: https://lazyadmin.nl/powershell/set-aduser/

    If you started with your script and need some more help please ask and post your script (using the CodeSample option of the Q&A editor for the script (the icon with 101010)) together with the error message or details what isn't working properly.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten