The script sets the owner of the Organizational Unit where a user account is located as the user's manager. To specify user managers using the script, you need to create business rule that run the script automatically once a user account is created in AD or moved to another Organizational Unit. For an example of a business rule that runs a script once a new user is created, see Run PowerShell Script after Creating a User.
PowerShell
# Bind to the OU where the user is located
$parent = $Context.BindToObject($Context.TargetObject.Parent)
try
{
# Get the owner's DN
$managerDN = $parent.Get("managedBy")
}
catch
{
$Context.LogMessage("Cannot update a manager for the user because the user's Organizational Unit is not managed by anyone", "Warning") # TODO: modify me
return
}
# Set manager
$Context.TargetObject.Put("manager", $managerDN)
$Context.TargetObject.SetInfo()