Update Manager Attribute on AD by Importing CSV file

Keeganz 1 Reputation point
2022-02-08T07:42:55.35+00:00

Hello,

How can I update the reporting manager attribute on AD based on the user's email address / UserPrincipalName instead of the user's name.

I have seen some examples online but still a bit confuse. Appreciate some help. Thank you !

CSV file sample
UserEmail ManagerFullname
john@keyman .com Tim Wang

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,959 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,396 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Clément BETACORNE 2,031 Reputation points
    2022-02-08T10:18:11.197+00:00

    Hello,

    You can do it what you will have to do is:

    1. Search for the manager user based on the email address
    2. Modify the target user manager with the previously user object you get from 1

    Below an example :

    $CSV = Import-Csv -path Example_csv.csv -Delimiter ";"  
    foreach($line in $CSV) {  
        $Manager = Get-ADUser -LDAPFilter "(mail=$($line.UserEmail))"  
        Set-ADUser xxx -Manager $Manager  
    }  
    

    My CSV look like this :
    UserEmail;ManagerFullname
    john@keyman .com;Tim Wang

    Regards,


  2. Clément BETACORNE 2,031 Reputation points
    2022-02-08T13:21:27+00:00
    $CSV = Import-Csv -Path Example_csv.csv -Delimiter ";"
    foreach($line in $CSV) {
        $Manager = Get-ADUser -Filter "Name -like '$($line.ManagerFullname)'"
        $TargetUser = Get-ADUser -Filter "UserPrincipalName -like '$($line.UserEmail)'"
        if(@($Manager).Count -lt 2) {
            Set-ADUSer $TargetUser -Manager $Manager
        } else {
            Write-Output "More than 1 user exist with this name $($line.ManagerFullname)"
        }
    }
    

    I posted it here to have the actual answer