PS Script to import AD computer objects attributes from CSV to AD? RRS feed

  • Question

  • Hi there, I have used the following script to export AD attributes to CSV. Once I fill CSV with necessary attributes value, which script should i use to export the CSV data back to AD? Is it same with Get replaced with Set? thanks 

    $Objects = Get-ADComputer -Server "1234" -SearchBase "OU=Servers,OU=xvy,DC=pqr,DC=123" -Filter * -Properties Name, company, department, managedBy, division, Description
    $Results = @()
    ForEach ($Object In $Objects)
        $Results += New-Object -Type PSObject -Property @{
            "Name" = $Object.Name
            "Company" = $Object.company
            "Department" = $Object.department
            "Division" = $Object.division
            "ManagedBy" = $Object.managedBy
    "Description" = $Object.Description
    $Results | Export-Csv -Path C:\Temp\test.csv -NoTypeInformation

    Friday, April 24, 2020 7:29 PM

All replies

  • The first problem is that none of the fields in the CSV file uniquely identifies the computers. The name is only unique in the OU and is not accepted by the Get-ADComputer cmdlet to identify the object. The script could search (filter) for the computer with the specified Name and retrieve the sAMAccountName, but this adds extra code. Best to replace Name with sAMAccountName, which uniquely identifies the computer in the domain.

    Something similar to below should work. There are parameters of the Set-ADComputer cmdlet to assign values to managedBy and description. There are not for the other attributes, so the -Replace parameter must be used. You pass a hash table of attribute name and value pairs, semicolon delimited, to this parameter, as below.

    $Computers = Import-Csv -Path C:\Temp\test.csv 
    ForEach ($Computer In $Computers)
        $ComputerID = $Computer.sAMAccountName
        $Company = $Computer.Company
        $Dept = $Computer.Department
        $Division = $Computer.Division
        $MgdBy = $Computer.ManagedBy
        $Descr = $Computer.Description
        Set-ADComputer -Identity $ComputerID -ManagedBy $MgdBy -Description $Descr -Replace @{company=$Company;department=$Dept;division=$Division}

    Help for Set-ADComputer here:


    You can also use the -WhatIf parameter of the cmdlet, which will show what the script will do without making any changes. Then remove the parameter to update AD.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, April 24, 2020 8:57 PM
  • Thank you Richard, the script was helpful. 
    Tuesday, April 28, 2020 5:10 PM