Is it possible to configure a dynamic distribution group to contain all direct reports of some person, plus all their direct reports recursively?
Verbose Info
To get the direct reports only I believe we'd just run:
Set-DynamicDistributionGroup -Identity 'SomeManagersDirectReports' `
-RecipientFilter "((RecipientType -eq 'UserMailbox') -and (Manager -eq 'CN=SomeManager,OU=Users,DC=domain,DC=example,DC=com')"
But to get a manager, their direct reports, and their reports recursively, the only way I can think of is to run something like the below to generate a list of members, then use that to update a (static) distribution group dynamically.
# very rough code to demo thinking... Haven't yet considered things like character escaping /
# circular loops / other fun things which may be found in the wild...
[string]$FirstPersonDn = 'CN=SomeManager,OU=Users,DC=domain,DC=example,DC=com'
[System.Collections.Generic.List[string]]$newMembers = [System.Collections.Generic.List[string]]::new()
[System.Collections.Generic.List[string]]$members = [System.Collections.Generic.List[string]]::new()
$newMembers.Add($FirstPersonDn)
while ($newMembers.Count) {
$members.Add($newMembers)
$newMembers = $newMembers | %{Get-AdUser $_ -properties DirectReports} | % DirectReports
}
Update-DistributionGroupMember -Identity 'SomeManagerAndTheirReportsRecusive' -Members $members