Powershell filter a list of names

Active Directory Filter doesn't support the -notin operator. You can use the following LDAP Filter trick to exclude those users from your query:

# $toExclude could be also pulled from a file, however you need to make
# sure there are no trailling or leading spaces on each line,
# you can use `.Trim()` for that.
#
# $toExclude = (Get-Content userstoexclude.txt).ForEach('Trim')

$toExclude = 'user.example1', 'user.example2', 'user.example3'
$filter = '(&(!name={0}))' -f ($toExclude -join ')(!name=')
# LDAP Filter would look like this:
# (&(!name=user.example1)(!name=user.example2)(!name=user.example3))

$userList = Get-ADUser -LDAPFilter $filter

If you're interested in learning more about LDAP Syntax for your queries you might want to check out:

  • Active Directory: LDAP Syntax Filters