Updating AD from csv

Hello,
I’m stuck and hopefully someone can give me some direction. Every 2 weeks I receive a .csv for HR. I use it to sync employee info to AD. I had a script that has been working:

foreach ($user in $users) {            
Get-ADUser -Filter "UserPrincipalName -eq '$($user.email)'" -Properties * -SearchBase "OU=someOU,DC=mydomain,DC=local" |            
  Set-ADUser -Division $($user.Division) -Department $($user.Department) -EmployeeNumber $($user.EmployeeNumber) -Title $($user.Job) 
}

I now need to add “manager” to AD. So, I did this:

foreach ($user in $users) {            
Get-ADUser -Filter "UserPrincipalName -eq '$($user.email)'" -Properties * -SearchBase "OU=someOU,DC=mydomain,DC=local" |            
  Set-ADUser -Division $($user.Division) -Department $($user.Department) -EmployeeNumber $($user.EmployeeNumber) -Title $($user.Job) -Manager $($user.Manager) 
}

However, ADUser -Manager only accepts 4 attributes. The only that makes sense for me is SamAccountName. Unfortunately, our SamAccountName does not match our UPN and it is not included in the .cvs I’m given. I do have the employee’s manager’s email address, which is the same as the UPN so, I did this this to get the SamAccountName of the employee’s manager:

foreach ($user in $users) {            
Get-ADUser -Filter "UserPrincipalName -eq '$($user.manageremail)'" -SearchBase "DC=mydomain,DC=local" -Properties  SamAccountName | Select SamAccountName
 }

This works and I get a list of each emplyee’s mager’s SamAccountName.
However, I cannot figure out have to feed these results into -Manager $($user.Manager)
Everything I tried has failed. Mostly because I’m not sure of the correct approach. Any help would be greatly appreciated!

If you want an inline solution:

foreach ($user in $users) {            
Get-ADUser -Filter "UserPrincipalName -eq '$($user.email)'" -Properties * -SearchBase "OU=someOU,DC=mydomain,DC=local" |            
  Set-ADUser -Division $($user.Division) -Department $($user.Department) -EmployeeNumber $($user.EmployeeNumber) -Title $($user.Job) -Manager $((Get-ADUser -Filter "UserPrincipalName -eq '$($user.manageremail)'" -SearchBase "DC=mydomain,DC=local" -Properties  SamAccountName).SamAccountName) 
}

Otherwise, for readability, you can do this:

foreach ($user in $users) {            
$Manager = (Get-ADUser -Filter "UserPrincipalName -eq '$($user.manageremail)'" -SearchBase "DC=mydomain,DC=local" -Properties  SamAccountName).SamAccountName
Get-ADUser -Filter "UserPrincipalName -eq '$($user.email)'" -Properties * -SearchBase "OU=someOU,DC=mydomain,DC=local" |            
  Set-ADUser -Division $($user.Division) -Department $($user.Department) -EmployeeNumber $($user.EmployeeNumber) -Title $($user.Job) -Manager $Manager
}

OK, thanks, Jeremy. I’ll give that a try.

That worked perfectly. Thanks so much for your help.