• ### Question

• Hello

I've been asked to update the title for users at our organization from a CSV file.  The CSV file contains theforst name, last name, and the updated title for each user.

I have no trouble importing the file - all headers are correct.

Where I'm having an issue is using a ForEach loop and trying to look up the ad accounts via first and last name

My code looks like this:

foreach($userin$users) {

$FirstName=$user.GivenName

$lastname=$user.Surname

$ADUser=Get-ADUser-Filter{Surname-eq'($lastname)'-andGivenName-eq'($firstname)'} -Properties* } However, this returns no values to the array$ADUser - if I plug a string (such as the user first name and last name) I get results back, but never when I try to pass the names through the l;oop using the variable.

I'm sure I'm missing something - any ideas?

Wednesday, October 26, 2016 7:45 PM

• YOu can also do it this way:

import-csv users.csv |
ForEach-Object{
Get-Aduser -Filter "surname -eq '$($_.surname)' -and givenname -eq '$($_.givenname)'"
}


\_(ツ)_/

• Proposed as answer by Wednesday, October 26, 2016 8:20 PM
• Marked as answer by Thursday, October 27, 2016 11:39 AM
Wednesday, October 26, 2016 8:09 PM

### All replies

Get-ADUser -LDAPFilter "(&(givenName=$firstname)(sn=$lastname))"

-- Bill Stewart [Bill_Stewart]

• Edited by Wednesday, October 26, 2016 7:52 PM
Wednesday, October 26, 2016 7:52 PM
• Proper use of filter:

$ADUser = Get-ADUser-Filter {Surname-eq '$lastname' -and GivenName-eq '$firstname'} -Properties* Assuming header of "Surname,Givenname" \_(ツ)_/ Wednesday, October 26, 2016 7:53 PM • If you're referring to removing the () from the variables, the results are still null And, yes, the headers are Surname and GivenName • Edited by Wednesday, October 26, 2016 8:03 PM Wednesday, October 26, 2016 8:02 PM • Your CSV file is not correct. \_(ツ)_/ Wednesday, October 26, 2016 8:04 PM • Then your header line must be incorrect. Is it comma delimited? Richard Mueller - MVP Enterprise Mobility (Identity and Access) Wednesday, October 26, 2016 8:06 PM • Yes, it's comma delimited header looks like so: Surname,GivenName,Title Information in the rest looks like: SMITH,JOHN,SYSTEMS ENGINEER JONES,SAM,SR SYSTEMS ENGINEER There are no spaces or whitespace in the first two rows - only in the title. - after import, I get the information correctly - just happens when I use the loop for each array member • Edited by Wednesday, October 26, 2016 8:19 PM Wednesday, October 26, 2016 8:09 PM • YOu can also do it this way: import-csv users.csv | ForEach-Object{ Get-Aduser -Filter "surname -eq '$($_.surname)' -and givenname -eq '$($_.givenname)'" }  \_(ツ)_/ • Proposed as answer by Wednesday, October 26, 2016 8:20 PM • Marked as answer by Thursday, October 27, 2016 11:39 AM Wednesday, October 26, 2016 8:09 PM • Throws an error - Get-Aduser : The search filter cannot be recognized At C:\scripts\in progress\UpdateTitle.ps1:17 char:3 + Get-Aduser -Filter "surname -eq '$($_.surname)' -and givenname -eq '$($_.given ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Thursday, October 27, 2016 11:32 AM • You have a typo in your version. We can't see your code so we cannot tell you where. \_(ツ)_/ Thursday, October 27, 2016 11:34 AM • My mistake - this works! - so, to change the title on each, would I pipe this to the following? Set-Aduser -Replace @{title='$(\$_.Title)'}

• Edited by Thursday, October 27, 2016 12:10 PM
Thursday, October 27, 2016 11:39 AM