Update AD attribute based on matching of another attribute?

In Powershell I am wondering the best way to achieve the updating of a custom attribute from a column in a CSV for users, based on the matching of a different column to a different attribute.

The spreadsheet essentially has a "last name" field that we know should match. Ideally I'd want to try and see if Powershell could:

1. Import the csv of a few hundred rows
2. Look up the user account in AD based on the matching of the EmployeeID and LastName columns in the spreadsheet
3. Update the other attribute (in this case it's a custom AD attribute called "personalEmail" to the corresponding "personalEmail" column, if the other columns match accordingly...
garryshapeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NinjaStyle82Systems AdministratorCommented:
Since i don't have a similar set up here i couldn't test my script, but something along these lines should do the trick.

$csv = pathtocsv
foreach ($user in $csv)
{
$aduser = get-aduser -filter {surname -eq $user.lastname -and employeeid -eq $user.employeeid} -properties personalemail 
$personalEmail = $user.personalEmail
$aduser | set-aduser -Replace @{personalEmail=$personalEmail}
}

Open in new window

0
NinjaStyle82Systems AdministratorCommented:
Try it with like 1 row to make sure it works before doing the full list :)
0
garryshapeAuthor Commented:
nice yeah and I'll add -whatif if applicable to the cmdlet style here
0
garryshapeAuthor Commented:
hmm seems I get error

Get-ADUser : Property: 'LastName' not found in object of type:
'System.Management.Automation.PSCustomObject'.
At line:1 char:26
+ foreach ($user in $csv) {Get-ADUser -Filter {surname -eq $user.LastName} }

I have the column labeled as "LastName", and I can run "foreach ($user in $csv) {$user.LastName}" to generate the lastnames,  so I'm not sure why
0
NinjaStyle82Systems AdministratorCommented:
shoot, I'm sorry, i see what i did.
$csv = import-csv pathtocsv
foreach ($user in $csv)
{
$aduser = get-aduser -filter {surname -eq $user.lastname -and employeeid -eq $user.employeeid} -properties personalemail 
$personalEmail = $user.personalEmail
$aduser | set-aduser -Replace @{personalEmail=$personalEmail}
}

Open in new window

add the import-csv before it's path.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.