How to get sam account name from employee id and update user records

Zeeshan Qaiser Opel 41 Reputation points
2022-10-30T12:02:57.22+00:00

I have a csv file of employee data in following format
empdata.csv

==================================================================================

empid,name,desig,dept,loc,grade,costcenter,division
5431,Zeeshan Qaiser Opel,Management Executive I,INFORMATION TECHNOLOGY,LAHORE,8,2260,INFORMATION TECHNOLOGY

==================================================================================

I need to get the employee id from the csv file, match it with user in AD, find the employees samaccountname and update the rest of the fields.

S
Script:

==================================================================================

Import-Module ActiveDirectory
Import-Csv "C:\empdata20.csv" |
ForEach-Object {
$EmpID=$.empid
$sam = {Get-ADUser -Filter "EmployeeID -eq '$($
.empid)'" | select samaccountname}

    Set-ADUser -Identity $sam  -Replace @{employeetype=$Type}  
    Set-ADUser -Identity $sam  -Replace @{title=$Desig}  
   }  

==================================================================================

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,208 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,453 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,756 Reputation points
    2022-10-30T14:28:36.597+00:00

    Try this:

    Import-Csv "C:\empdata20.csv" |  
        ForEach-Object {  
            $sam = (Get-ADUser -Filter "EmployeeID -eq '$($_.empid)'").samaccountname  
            if ($sam){  
                Set-ADUser -Identity $sam  -Replace @{employeetype = $Type; title = $Desig }  
            }  
            else{  
                Write-Host "No user was found with employeeid '$($_.empid)'"  
            }  
        }  
      
    
    0 comments No comments

0 additional answers

Sort by: Most helpful