0
votes

I have a csv file with people data. One row per person. I want to update Active Directory with the data using powershell, but only the first row is updated in Active Directory.

How can I modify the code to update all the row values?

Import-Module ActiveDirectory
Import-Csv "C:\code.csv" -Delimiter ';' -Encoding "UTF8" |
ForEach-Object
{
    $params = @{Identity = $_.SamAccountName}
    if (-not [string]::IsNullOrWhiteSpace($_.Surname))
    {
        $params.Surname = $_.Surname
    }
    if (-not [string]::IsNullOrWhiteSpace($_.Givenname))
    {
        $params.Givenname = $_.Givenname
    }
    if (-not [string]::IsNullOrWhiteSpace($_.DisplayName))
    {
        $params.DisplayName = $_.DisplayName
    }
    if (-not [string]::IsNullOrWhiteSpace($_.Title))
    {
        $params.Title = $_.Title
    }
    if (-not [string]::IsNullOrWhiteSpace($_.Department))
    {
         $params.Department = $_.Department
    }
    if (-not [string]::IsNullOrWhiteSpace($_.Manager)) 
   {
        $params.Manager = $_.Manager
   }
   if (-not [string]::IsNullOrWhiteSpace($_.OfficePhone)) 
   {
       $params.OfficePhone = $_.OfficePhone
   }
   if (-not [string]::IsNullOrWhiteSpace($_.MobilePhone)) 
   {
        $params.MobilePhone = $_.MobilePhone
   }
}
Set-ADUser @params

The csv file:

SamAccountName;surname;givenname;displayname;Job Title;Department;Manager;HomePhone;MobilePhone
test1;Test;1;Test 1;;;;;
test2;Test;2;Test 2;something;anything;test1;136 / 8022;-1079
1

1 Answers

2
votes

Your doing the Set-ADUser after the foreach-object, this means you're looping through collecting the information, replacing it line by line, then only 'setting' the final bit.

Set-ADUser needs to be in the foreach if you want it to do it for each line.