All users in OU report to.

by Jordan at 2013-03-04 10:01:35

Looking for a script that will automatically or manually change users Manager based on the specific OU a user is in while excluding the Manager.

Thanks in advance.
by mikefrobbins at 2013-03-04 15:01:16
This is for a single OU. The "ManagedBy" property on the "Adventureworks Users" OU in the following example has been set to the user who manages the users contained in that OU.

$Manager = Get-ADOrganizationalUnit -Filter {Name -eq 'Adventureworks Users'} | select -expand ManagedBy
Get-ADUser -Filter {DistinguishedName -ne $Manager} -SearchBase 'OU=Adventureworks Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' |
Set-ADUser -Manager $Manager -PassThru | select name

Here’s a one liner I wrote a while back using the Quest AD cmdlets to set the IT departments manager based on Department and Title:
Get-QADUser -Department 'Information Technology' -Enabled |
where {$.title -ne 'IT Director'} |
Set-QADUser -Manager (Get-QADUser -Title 'IT Director')
by mikefrobbins at 2013-03-04 17:07:02
This should do the trick, but test it thoroughly in a test (non-production) environment. You’ll need to set the ManagedBy setting for each OU for the script to know who the manager is. You could easily modify the script to be based off of another property if needed.

Get-ADOrganizationalUnit -Filter * | ForEach-Object {
If ($
) {Get-ADUser -Filter {DistinguishedName -ne $.ManagedBy} -SearchBase $.DistinguishedName -SearchScope OneLevel |
Set-ADUser -Manager $.ManagedBy}
Else {Write-Output "Managed By Not Set for $