Categories
Powershell Technology

Update Active Directory Users from csv file

 $userUpdate = Import-Csv -Delimiter "," -Path ("c:\FaxNumberUpdate.csv")
$rowCount = 1
foreach($user in $userUpdate){
    Write-Host  ($rowCount.ToString() + ") Update " + $($user.DisplayName) + " to " + $($user.Fax))
    # Find user
    $ADUser = Get-ADUser -Filter "displayname -eq '$($user.name)'" 

    if ($ADUser){
        Set-ADUser -Identity $ADUser -Fax $user.fax
        Write-Host  ($rowCount.ToString() + ") Completed Update " + $($user.name) + " to " + $($user.fax))
    }else{
        Write-Warning ("Failed to update " + $($user.name))
    }
    $rowCount = $rowCount + 1
}
 

The idea is to have a CSV file with column headers, and powershell reads the CSV file into memory, assigns the memory a label called $userUpdate. Then iterates through the memory label $userUpdate row by row finding a matching AD user (Get-ADUser -Filter) matching the AD value called displayname to the spreadsheet column cell being read with the header named “name”. Its case sensitive. Set-ADUser does the actual update updating the matched $ADUser.

Leave a Reply