I found myself needing a simple function that would return true or false based on whether a specified username is in use or not.  I needed this for validating username parameters in my other functions, as well for my function that would generate a valid username from a first, middle, and last name that wasn't already in use.  Based on some code found here: https://www.petri.com/test-active-directory-user-accounts-with-powershell

Full description can be found here: http://ryandevries.com/index.php/2015/06/02/test-an-active-directory-user

PowerShell
Edit|Remove
FUNCTION Test-ADUser { 
<# 
.SYNOPSIS 
    Test an Active Directory User Account 
.DESCRIPTION 
    This command will test if a given Active Directory user account exists. 
.PARAMETER Username 
    The name of an Active Directory user account. This should be either the samAccountName or the DistinguishedName. 
.PARAMETER  Log 
    Optional, a path for a log file 
.EXAMPLE 
    PS C:\> Test-ADUser rdevries 
    True 
.NOTES 
    Author      : Ryan DeVries 
    Last Updated: 2015/05/19 
    Version     : 1 
.LINK 
    https://www.petri.com/test-active-directory-user-accounts-with-powershell 
.INPUTS 
    [string] 
.OUTPUTS 
    [Boolean] 
#> 
    [CmdletBinding()] 
    Param( 
        [Parameter(Position=0,Mandatory,HelpMessage="Enter an AD user name",ValueFromPipeline)] 
        [ValidateNotNullorEmpty()] 
        [string]$Username, 
        [Parameter(Position=1,Mandatory=$false,HelpMessage="Enter the path of a log file to use")] 
        [ValidateScript({Test-Path (Split-Path $_ -Parent) -PathType Container})] 
        [string]$Log 
    ) 
  
    begin { # Configure environment, set up logging, initialize variables 
        if (!(Get-Module ActiveDirectory)){ Import-Module ActiveDirectory } 
        Write-Verbose "Starting $($MyInvocation.Mycommand)"   
        Write-Verbose "Detected parameter set $($PSCmdlet.ParameterSetName)" 
        Write-Verbose ($PSBoundParameters | out-string) 
        $newlog = $false 
        if ($log){ if(!(Test-Path $log -PathType Leaf)){ $newlog = $true } } 
        if ($log){  
            if ($newlog){  
                Write-Verbose "Starting log at $log" 
                Start-Log -Path (Split-Path $log -Parent) -Name (Split-Path $log -Leaf) 
            }  
            $scriptstring = "Starting $($MyInvocation.MyCommand)" 
            foreach ($param in $PSBoundParameters.GetEnumerator()){ $scriptstring += " -$($param.key) $($param.value)"} 
            Write-Log -Path $log -Line $scriptstring 
        } 
    } 
  
    process { 
        Write-Verbose "Searching for user $username" 
        if ($log){ Write-Log -Path $log -Line "Trying $username" } 
        # Tries to get information about the specified user, returning true if found and false if it catches the specific ADIdentityNotFound exception, terminating if it finds any other error 
        try { 
            $user =  Get-ADUser -Identity $username -ErrorAction Stop 
            if ($log){ Write-Log -Path $log -Line "Found: $(($user | out-string).TrimEnd())`r`n" } 
            Write-Verbose "Found user : $(($user | out-string).TrimEnd())" 
            $true 
        }  
        catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {  
            if($log){ Write-Log -Path $log -Line "User $username does not exist" } 
            Write-Verbose "User $username does not exist" 
            $false 
        } 
        catch { 
            #handle all other errors 
            if ($log){ Write-Log -Path $log -Line "ERROR : $($_.Exception)" } 
            Write-Verbose "ERROR : $($_.Exception)" 
            throw $_ 
            return 
        } 
    } 
  
    end {  
        if ($log){ if($newlog) { Stop-Log -Path $log } } 
        Write-Verbose "Ending $($MyInvocation.Mycommand)"  
        Remove-Variable user -ErrorAction SilentlyContinue 
    } 
}