Updating Updating Active Directory Manager Attribute using PS from a CSV File

Anonymous
2021-10-20T22:17:10.95+00:00

I'm trying to update the AD manager field using PS. We we're given a CSV file from HR with everyone's manager info. This is PS script.

Import-Module ActiveDirectory
$Users = Import-Csv "C:\Users\Administrator.APC-SERVICES\Desktop\mngimport\Managers1.csv"
ForEach ($User in $User)
{
$ADUser = Get-ADUser -Filter "displayname -eq '$($User.ProfessionalFullName)'"
$manager = (Get-ADUser -Filter "displayname -eq '$($User.'ManagerFullName')'").distinguishedname

if ($ADUser -and $manager) {
Set-ADUser -Identity $ADUser -Replace @{manager = $manager }
}
}

Nothing happens after running this. I keep checking the AD Users and the Manager attribute does not update. What am i missing?

CSV format:
ProfessionalFullName,ManagerFullName
Greg Brown,Jay Smith
Jesus Lopez ,Jay Smith
Stephen Jones,Jay Smith
Scott Williams ,Roy Miller
Jason Davis,Roy Miller

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,354 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Rich Matheisen 44,621 Reputation points
    2021-10-21T02:08:47.977+00:00

    Try it this way:

    Import-Csv "C:\Users\Administrator.APC-SERVICES\Desktop\mngimport\Managers1.csv" |
        ForEach-Object {
            $ADUser  =  Get-ADUser -Filter "displayname -eq '$($_.ProfessionalFullName)'"
            $manager = (Get-ADUser -Filter "displayname -eq '$($_.ManagerFullName)'").distinguishedname
    
            if ($ADUser -and $manager) {
                $ADUser | Set-ADUser -manager $manager
            }
        }
    

    Two things:

    1. You don't have to explicitly import the ActiveDirectory module. PowerShell (since version 3) will do that automatically.
    2. Using the DisplayName to identify users is a little bit risky since a display name may not be unique in the AD forest. A samaccountname or primary email address is much better
    2 people found this answer helpful.

  2. Anonymous
    2021-10-22T19:46:40.263+00:00

    Hey,

    Is look like half of my users failed to updated. I modified it for "sAMAccountName" and the "Name".

    This is what I'm running:

    Import-Csv "C:\Users\Administrator.APC-SERVICES\Desktop\mngimport\managers.csv" |
    ForEach-Object {
    $ADUser = Get-ADUser -Filter "Name -eq '$($.Name)'"
    $manager = (Get-ADUser -Filter "sAMAccountName -eq '$($
    .sAMAccountName)'").distinguishedname
    }
    if ($ADUser -and $manager) {
    $ADUser | Set-ADUser -manager $manager
    }

    CSV File:
    Name,sAMAccountName
    Dipal Smith,VBell
    Dolly Ryan,VBell

    Any thoughts?