Test AD User Exists

by May 9, 2018

If you’d need to find out whether a given Active Directory user exists, and provided you have installed the ActiveDirectory PowerShell module which is part of RSAT (Remote Server Administration Toolkit), here is a clever way:

function Test-UserExists
{
    param
    (
        [Parameter(Mandatory)]
        [string]
        $SAMAccountName 
    )
 
    @(Get-ADUser -LDAPFilter "(samaccountname=$SAMAccountName)").Count -ne 0
 
}

You can adjust the LDAP query to check for users based on other attributes as well.

Twitter This Tip! ReTweet this Tip!