Public/Get-Accounts.ps1

Function Get-Accounts {
    <#
        .SYNOPSIS
        Gets a list of all associated accounts to the user found with the given search term
         
        .DESCRIPTION
        Provide a search term, and this function will search for a user account, then using the EmployeeID from that account it will find all related accounts (T0, T1, User)
         
        .PARAMETER SearchTerm
        Specifies the file name.
 
        .SYNTAX
 
        .INPUTS
        System.Object
            Properties
                SamAccountName (Active Directory Login Name)
                DistinguishedName (Active Directory Object Distinguished Name)
                EmployeeID (Team Member ID)
                mail (Email Address)
 
        System.String
            SearchTerm
                SamAccountName (Active Directory Login Name)
                DistinguishedName (Active Directory Object Distinguished Name)
                EmployeeID (Team Member ID)
                mail (Email Address)
 
        .OUTPUTS
        System.Object
            The found account
         
        [array]System.Object
            The list of found accounts
 
        .EXAMPLE
        PS> Get-UsersAccounts -SearchTerm ""
 
        .EXAMPLE
        PS> Get-UsersAccounts TMID
 
        .EXAMPLE
        PS> Get-UsersAccounts -SearchTerm ""
 
        .LINK
        Online version:
 
        .LINK
        Set-Item
    #>

    [cmdletBinding()]
    Param(
        [Parameter(
            ValueFromPipelineByPropertyName
        )]
        $EmployeeID,
        $Search = $ENV:USERNAME,
        [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")
    )
    BEGIN { }
    PROCESS {
        If ($EmployeeID) {
            Write-Verbose "Searching $EmployeeID"
            Get-ADUser -Filter "EmployeeId -like '$EmployeeID*'" -Properties $Properties | Select-Object $Select
        }
        elseif ($Search) {
            foreach ($SearchTerm in $Search) {
                Write-Verbose "Getting all accounts that match $SearchTerm"
                $Accounts = Get-AdUser -Filter "SamAccountName -eq '$SearchTerm' -or UserPrincipalName -eq '$SearchTerm' -or employeeid -eq '$SearchTerm'" -Properties "$Properties"
                Foreach ($Account in $Accounts) {
                    $EmployeeID = $Account.EmployeeID
                    Get-ADUser -Filter "EmployeeId -like '$EmployeeID*'" -Properties "$Properties" | Select-Object $Select
                }
            }
        }
    }
    END { }
}