AD acoount info update by powershell

Naveen Kumar 21 Reputation points
2022-04-22T21:26:29.033+00:00

From samaccountname and i need to update these 4 attributes: EmployeeID, Title, Manager, Department

I have a CSV file (c:\temp\adinfo.csv) with fields:
samaccountname, EmployeeID, Title, Manager, Department

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
4,320 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.
4,891 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 68,296 Reputation points MVP
    2022-04-23T00:42:37.097+00:00

    Hi @Naveen Kumar ,

    you didn't mention which attribute you are using for the Manager.
    Assuming it's the SamAccountName as well this might help (not tested by myself).

    Import-Csv "c:\temp\adinfo.csv" |  
    ForEach-Object {  
      $userObj = Get-ADUser -Filter "SamAccountName -eq '$($_.SamAccountName)'"  
      $managerObj = Get-ADUser -Filter "SamAccountName -eq '$($_.Manager)'"    
      if ($userObj -and $managerObj) {  
        $userObj | Set-ADUser -Manager $managerObj -Title $_.Title -EmployeeID $_.EmployeeID -Department $_.Department  
      }  
    }  
    

    ----------

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

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Rich Matheisen 38,831 Reputation points
    2022-04-23T02:30:16.107+00:00

    This does a bit of data validation before updating the user. The assumption is that you're intention is to not replace existing values with and empty value if the CSV omits a value.

    A second assumption is that the "manager" value in the CSV is either the manager's samaccountname or distinguished name (a GUID or SID is also acceptable, but it's unlikely you're using one of these).

    Import-Csv -Path c:\temp\adinfo.csv | 
        ForEach-Object {
            $samacct = $_.samaccountname        # needed for exception handling
            if ($_.samaccountname.Trim().Length -gt 3) {
                Try{
                    $u = Get-ADUser -Identity ($_.samaccountname.trim()) -ErrorAction STOP
                    $props = @{ Identity = $_.samaccountname.trim() }
                    if ($row.Title.trim().Length -gt 1){
                        $props['Title'] = $_.Title.trim()
                    }
                    if ($row.Manager.trim().Length -gt 1){
                        $props['Manager'] = $_.Manager.trim()
                    }
                    if ($row.Drpartment.trim().Length -gt 1){
                        $props['Department'] = $_.Department.trim()
                    }
                    if ($row.Drpartment.trim().Length -gt 1){
                        $props['EmployeeID'] = $_.EmployeeID.trim()
                    }
                    Try{
                        Set-ADUser @props -ErrorAction STOP
                    }
                    Catch{
                        Write-Host "Failed to update user with samaccountname '$samacct'." -ForegroundColor Yellow    
                    }
                }
                Catch{
                    Write-Host "User with samaccountname '$samacct' was not found." -ForegroundColor Yellow
                }
            }
            else {
                Write-Host "User's samaccountname is either empty or too short: '$samacct'"
            }
        }
    
    0 comments No comments