question

efrontsang-1018 avatar image
0 Votes"
efrontsang-1018 asked AndreasBaumgarten commented

Bulk edit AD Users Employee ID

I'm trying to edit a bulk of users that I have their samaccountname in a CSV. The attrib that I need to update is EmployeeID
I have zero coding skill
my CSV file looks like this

logon;EmployeeID
User1@company.local;12345678

validate whether or not the employeeid attribute is set and then only set it if it's blank otherwise skip it.

i found some code from google but it is not working

 $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 "c:\UserList-Results.csv"

Any tips ? thanks !!

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @efrontsang-1018 ,

did the answer work for you? Are there any additional questions to this topic?

If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A

----------
Regards
Andreas Baumgarten

0 Votes 0 ·

1 Answer

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @efrontsang-1018 ,

if you the column ist named logon and contains the samaccountname your script in line 4 should use the filter samaccountname and $_.logon

Something like this:

 $User = Get-ADUser -Filter "SamAccountName -eq $_.logon" 


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.