PowerShell: Update Manager Active Directory Field based on Employee Number

This PowerShell script can be used for example when based on a CSV export or SQL database of HR data contains a field that is called  ‘manager’ in which the employee Number of the manager’s name is used to identify the manager of a person, instead of the manager’s name. The ‘manager’ attribute of the Active Directory user account will be filled with the user account name of the manager.

In this specific case the variable $MgrEmpNr was filled by RES ONE Service Store, based on a SQL database View based on a column within that view that held the manager’s employee number and was mapped to it.

The variable $UserName was filled with a so called RES ONE Service Store person attribute that was loaded during the subscription of the user’s automatic request to update the manager attribute.

This script can also be adjusted and used to update the attributes as well, if the Active Directory user name and the manager’s employee number are known.

#Name: Alex Goris
#Version: 0.1
#Purpose: Search manager’s name with EmployeeNumber of manager given and set manager field in Active Directory

Import-Module ActiveDirectory #Import Active Directory module

#Set Attributes

$UserName = “jdoe” #Set variable for the user’s logon name of whom manager’s field needs to be filled

$MgrEmpNr = “2222” #Set variable for the manager’s employeeNumber

$Manager = Get-ADUser -LDAPFilter “(employeeNumber=$MgrEmpNr)” #Set variable  Manager and fill with the manager’s name based on the manager’s Active Directory field employeeNumber

Set-ADuser $UserName -manager $Manager.DistinguishedName #Fill Manager field of UserName with the DN


Leave a Reply

Your email address will not be published. Required fields are marked *