Update EmployeeID Attribute based on csv

Here is the code I used to update the employeeID attribute based on csv

$Users = Import-CSV "C:\PSS\UserList.csv"
$Results = $Users | ForEach-Object {
    try {
        $User = Get-ADUser -Filter {mail -eq $_.mail}
        if ($null -ne $User -and $null -eq $_.EmployeeID) {
            try {
                Set-ADUser $User.SamAccountName -EmployeeID $_.EmployeeID
                $_ | Add-Member -MemberType NoteProperty Status -Value "Complete"
            } catch {
                $ErrorMessage = $_.Exception.Message
                $_ | Add-Member -MemberType NoteProperty Status -Value $ErrorMessage
            }
        } else {
            $_ | Add-Member -MemberType NoteProperty Status -Value "Skipped"
        }
    } catch {
        $ErrorMessage = $_.Exception.Message
        $_ | Add-Member -MemberType NoteProperty Status -Value $ErrorMessage
    }
}
$Failed = $Results | Where-Object {$_.Status -ne "Complete" -and $_.Status -ne "Skipped"}
Write-Host "Failed Accounts: $($Failed.Count)"
$Results | Export-Csv -Path "UserList-Results.csv"

Here is my excel file

I ran this code but the output in csv is empty and employeeid attribute does not change. Any suggestions?

You never pass the object back in the pipeline. Add -Passthru parameter to your Add-Member lines or just output $_ as the very last line in the foreach-object loop

troy,
Welcome to the forum. :wave:t4:

My first suggestion would be not to post pictures if it doesn’t serve any purpose. :smirk:

For a try catch block to work properly you need a terminating error inside the try block to get the catch block activated. If the command you run inside the try block does not throw a terminating error by default you have to force it with the common parameter -ErrorAction Stop.

Why do you actually search for the users with their email address when you seem to have the sAMAccountName in your CSV file?
I’d use

$User = Get-ADUser -Identity $_.sAMAccountName

Next: I don’t get this logic:

If you DON’T have an employeeID in your CSV you want to set this non existing empoyeeID in your AD? Or did I get something wrong?