Public/Get-UserTeam.ps1

Function Get-UserTeam {
    <#
.SYNOPSIS
    This Function Gets a list of all AD Objects that share either the manager DistinguishedName provided, or the Manager property of the user object found with the provided search term
.NOTES
    Name: Get-UserTeam
    Author: Luke Hagar
    Version: 1.0
    DateCreated: 5/12/2021
.EXAMPLE
    Get-UserTeam -Identity "Luke.Hagar"
 
.EXAMPLE
    Get-User Luke.Hagar | Get-UserTeam
 
#>

    [CmdletBinding()]
    param (
        [Parameter(
            ValueFromPipelineByPropertyName,
            Position = 0
        )]
        [Alias("SamAccountName", "DistinguishedName", "GUID", "SID", "EmployeeID", "mail", "UserPrincipalName")]
        $Identity = $ENV:USERNAME,
        [Parameter(
            ValueFromPipelineByPropertyName
        )]
        $Manager,
        [string[]]$Properties = @("Name", "EmployeeID", "physicalDeliveryOfficeName", "Title", "mail", "msRTCSIP-PrimaryUserAddress", "CanonicalName", "DistinguishedName", "samaccountname", "UserPrincipalname", "AccountExpirationDate", "Enabled", "Manager", "badPwdCount", "LastBadPasswordAttempt", "LockedOut", "LockOutTime", "lastLogonDate", "PasswordExpired", "PasswordLastSet", "whenCreated", "whenChanged"),
        [string[]]$Select = @("Name", "EmployeeID", "physicalDeliveryOfficeName", "Title", "mail", "msRTCSIP-PrimaryUserAddress", "CanonicalName", "DistinguishedName", "samaccountname", "UserPrincipalname", "AccountExpirationDate", "Enabled", "Manager", "badPwdCount", "LastBadPasswordAttempt", "LockedOut", "LockOutTime", "lastLogonDate", "PasswordExpired", "PasswordLastSet", "whenCreated", "whenChanged"),
        [switch]
        $SubTeams
    )
    BEGIN {}
    PROCESS {
        if ($null -ne $Manager) {
            Write-Verbose "Searching for team based on Manager ($Manager)"
            $Users = Get-ADUser -Filter "manager -eq '$Manager'" -Properties $Properties | Select-Object $Select
            Return $Users
        }
        elseif ($Identity) {
            $Users = @()
            foreach ($SearchTerm in $Identity) {
                Write-Verbose "Searching for team based on Search Term ($SearchTerm)"
                $Manager = (Get-ADUser -Filter "employeeid -eq '$SearchTerm' -or SamAccountName -eq '$SearchTerm' -or DistinguishedName -eq '$SearchTerm' -or GUID -eq '$SearchTerm' -or SID -eq '$SearchTerm' -or EmployeeID -eq '$SearchTerm' -or mail -eq '$SearchTerm' -or UserPrincipalName -eq '$SearchTerm'" -Properties Manager).Manager
                $Users += Get-ADUser -Filter "manager -eq '$Manager'" -Properties $Properties | Select-Object $Select
            }
        }
        if ($SubTeams) {
            $SubTeamsUsers = $Users
            foreach ($User in $SubTeamsUsers) {
                $Manager = $User.DistinguishedName
                $Users += Get-ADUser -Filter "manager -eq '$Manager'" -Properties $Properties | Select-Object $Select
            }
        } 
        Return $Users     
        else {
            Write-Warning "No Search Info Provided"
        } 
    }
    END {}
}