# Update EmployeeID Attribute based on csv

**URL:** <https://forums.powershell.org/t/update-employeeid-attribute-based-on-csv/18972>\
**Category:** PowerShell Help\
**Created:** [March 31, 2022, 8:36am UTC](https://forums.powershell.org/t/update-employeeid-attribute-based-on-csv/18972 "2022-03-31T08:36:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![troy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/troy/32/2248_2.png) [@troy](https://forums.powershell.org/u/troy)\
**Post date:** [March 31, 2022, 8:36am UTC](https://forums.powershell.org/t/update-employeeid-attribute-based-on-csv/18972/1 "2022-03-31T08:36:31Z")

</div>

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

```auto
$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

 ![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/3/31118f57359272cfb07d13f9dad4319c8ba19922.png)

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

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [March 31, 2022, 9:00am UTC](https://forums.powershell.org/t/update-employeeid-attribute-based-on-csv/18972/2 "2022-03-31T09:00:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [March 31, 2022, 9:08am UTC](https://forums.powershell.org/t/update-employeeid-attribute-based-on-csv/18972/3 "2022-03-31T09:08:57Z")

</div>

troy,  
Welcome to the forum. 👋🏽

> [@troy](#):
>
> Any suggestions?

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

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

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

```

Next: I don’t get this logic:

> [@troy](#):
>
> `if ($null -ne $User -and $null -eq $_.EmployeeID) {`

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?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:18pm UTC](https://forums.powershell.org/t/update-employeeid-attribute-based-on-csv/18972/4 "2024-05-16T20:18:06Z")

</div>


