Private/ADLookups/_GetContactName.ps1

function _GetContactName {
    param (
        [parameter(Mandatory = $true)]
        [string]$NameToSearch,

        [parameter(Mandatory = $true)]
        [ValidateSet('*Search', '*Search*', 'Search*')]
        [string]$SearchStyle
    )

    switch ($SearchStyle) {
        '*Search' {
            $Query = "*$NameToSearch"
        }
        '*Search*' {
            $Query = "*$NameToSearch*"
        }
        'Search*' {
            $Query = "$NameToSearch*"
        }
    }

    if ($null -eq $AllContacts) {
        Return "Go to the File menu and click 'Update Object Cache'."
    }
    else {
        $Object = @()
        $Search = @()

        $Search += $AllContacts | Where-Object name -Like $Query
        $Search += $AllContacts | Where-Object mail -Like $Query

        foreach ($Contact in $Search) {
            $ContactObject = [PSCustomObject] @{
                Name              = $Contact.CN
                DistinguishedName = $Contact.DistinguishedName
            }
            $Object += $ContactObject
        }
        Return $Object | Select-Object -Unique -First 20
    }
}