简体   繁体   中英

How to remove terminated manager's DirectReports from Active Directory through PowerShell

I created a script to clear terminated user's manager in Active Directory. But want to remove his direct reportees through PowerShell

The Reports attribute is a linked attribute, and its forward link is the Manager attribute.

Remove (or replace) the manager in the Manager attribute of the users and the Reports values will disappear automatically

I use this script to clear Direct Reports from all users in a specific OU. It creates a list of the Manager's direct reports, and then loops through that list and nulls the Manager property. Run the script with -WhatIf to see the accounts that will be affected.

$TSManagerList = (Get-ADUser -Filter * -SearchBase "OU=Tombstone,DC=Contoso" -Properties directreports, description | where{$_.directreports -ne ""}).samaccountname | sort

foreach($TSManager in $TSManagerList)
{
    $DirReportList = (Get-ADUser $TSManager -Properties directreports).directreports
    foreach($DirReport in $DirReportList)
    {
        $DirReportSam = (Get-ADUser -Filter * | where{$_.distinguishedname -eq $DirReport}).samaccountname
        Set-ADUser -Identity $DirReportSam -Manager $null -WhatIf 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM