New PS user having issue with simple AD update

Let me preface this post with the fact that I’m extremely green when it comes to PS. (As in, today is my first day using it.) I’ve been tasked with updating a large number of AD user accounts with charge codes. It was decided that we should use the Department field for this. Unfortunately, the csv that was provided to me comes from an ancient Intranet where the only information that is useful that matched up to AD is the user’s employee number and charge code. The csv has the headers of User and Department. I know this should be a simple script, but I’m still getting my feet wet here, and am stumbling around in the dark, as it were. Any insight would be much appreciated.

$Userdata = Import-Csv C:\empID.csv
ForEach ($User in $Userdata) {
  $User = $User.User 
  $Department = $Department.Department
  Get-ADUser -Filter "EmployeeNumber -eq '$User'" -Properties * |
  set-AdUser -Add "'$User' -eq 'Department'"  -Properties Department

So, your first problem is that you’re using $User in your ForEach, and then “overwriting” that on line 3. I would rewrite lines 3, 5, and 6 to use something other than $User. E.g., “$EmployeeNumber = $User.user” or something.

Second, I’m not sure where you’re getting $Department.Department from. I suspect you meant $User.Department, since $User represents the current line of the CSV file. Well, it will if you stop overwriting it in line 3.

You’re using -Add incorrectly, and I’m not sure you actually should be using it at all. Department is a single-valued field in AD, and it already exists. And -Add isn’t intended to take a comparison.

Set-ADUser -Department $Department

That should set the Department attribute for whatever user Get-ADUser spat out.

Much obliged, sir. I got it ironed out.