We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Move user to a specific Organizational Unit

February 15, 2023 Views: 2792

This PowerShell script moves a user to an Organizational Unit whose distinguished name (DN) is specified by a certain property of the user's account. To execute the script, create a business rule triggered after creating or updating a user that will be triggered when the property is changed.

Parameters:

  • $targetOuProperty - Specifies the name of the property that will be used to specify the DN of the OU.
Edit Remove
PowerShell
$targetOuProperty = "adm-CustomAttributeText1" # TODO: Modify me

# Get the target OU DN
$targetOuDN = $Context.TargetObject.Get($targetOuProperty)

# Bind to the target OU
try
{
    $targetOU = $Context.BindToObjectByDN($targetOuDN)
}
catch
{
    $Context.LogMessage("The target location '$targetOuDN' does not exist.", "Error")
    return
}

# Move the user
try
{
    $targetOU.MoveHere($Context.TargetObject.AdsPath, $NULL)
}
catch
{
    $Context.LogMessage($_.Exception.Message, "Error")
    return
}

# Clear the property
$Context.TargetObject.Put($targetOuProperty, $NULL)
$Context.TargetObject.SetInfo()
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers