Updating all user AD settings in a loop – error checking

by Oct 2, 2015

Hi All,

I've been given a list from HR of: FirstName, Surname, Username, Title. The username field I've generated based on first initial + surname from the list. I need to import the titles into AD.

My concern is that there will be instances where the username generated from the HR list doesn't match the username in AD, for example if there is already a user in AD for Joe Bloggs called jbloggs, then if we get a 2nd user called John Bloggs their username might be jobloggs. In which case I assume the script will just fail.

I've written and tested the following code which is working when using 2 staff that I know usernames match between the HR list and AD:

$HcList = Import-Csv -Path Z:ScriptsPSADJobTitletest_titles.csv
$HcList | ForEach-Object {Set-ADUser -Identity $_.username -Replace @{Title=$_.Title}}

Any tips on how I might add in error checking, that is, I basically just want to bypass any in the HR list that don't directly match an AD username and allow the script to continue to loop with the rest of the users that do match without failing out to an error message.

Thanks guys!!