AD Bulk users of description with account name form CSV Import

Pedro Tomazi 21 Reputation points
2022-02-10T03:07:03.98+00:00

I want to be able to add/update the description attribute in AD from a CSV import, the CSV will have two columns: sAMAccountName and description, so I want the script to be able to search using the user account and then add the corresponding description. I have no scripting skills and I only have the script below starting point, can anyone help and see where I went wrong.

Import-Csv "C:\tmp\users.csv" | foreach {Set-ADUser - Identity = $_.sAMAccountName –description $_.description}

Thank you in advance.

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,403 questions
0 comments No comments
{count} votes

Accepted answer
  1. Clément BETACORNE 2,031 Reputation points
    2022-02-10T14:48:47.293+00:00

    Strange I don't have any error regarding the filter on my side :(
    Anyway you can try it like that :

    $CSV = Import-Csv -Path Example_csv.csv -Delimiter ";"
     foreach($line in $CSV) {
         $ADUser = Get-ADUser -identity $line.sAMAccountName
         if($ADUser -ne $null) {
             Set-ADUser $ADUser -Description $line.Description
         }
     }
    

1 additional answer

Sort by: Most helpful
  1. Clément BETACORNE 2,031 Reputation points
    2022-02-10T09:30:42.177+00:00

    Hello,

    You have an issue in your because the identity parameter will expect something like :

    • A distinguished name
    • A GUID (objectGUID)
    • A security identifier (objectSid)
    • A SAM account name (sAMAccountName)

    And you are giving this parameter something that does not look like what it is expecting.
    Below you will find an example of what you can do to update the description :

    $CSV = Import-Csv -Path Example_csv.csv -Delimiter ";"
    foreach($line in $CSV) {
        $ADUser = Get-ADUser -Filter "sAMAccountName -like '$($line.sAMAccountName)'"
        if($ADUser -ne $null) {
            Set-ADUser $ADUser -Description $line.Description
        }
    }
    

    Regards,