Avatar of Ben S
Ben S

asked on 

update AD users from csv with blank cells


I need to update around 1000 users from a csv, I have the script below which runs until it gets to an empty cell then I get an error, can anyone suggest away to stop the error?

#Import AD Module             
Import-Module ActiveDirectory            

write-Host 'Starting to update AD Attributes.......' -NoNewline -ForegroundColor Yellow            
# Import CSV into variable $users           
$users = Import-Csv -Path C:\temp\users.csv            
# Loop through CSV and update users if the exist in CVS file            
foreach ($user in $users) {            
#Search in specified OU and Update existing attributes            
 Get-ADUser -Filter "displayName -eq '$($user.displayName)'" -Properties * -SearchBase "OU=Users,DC=company,DC=local" |            
  Set-ADUser -Title $($user.Title) -Department $($user.Department) -MobilePhone $($user.MobilePhone) -OfficePhone $($user.OfficePhone) -Office $($user.Office) -City $($user.City)

Write-Host 'done!' -ForegroundColor Green

Open in new window


Avatar of undefined
Last Comment

8/22/2022 - Mon