Skip to content

/flags/active-directory/get-aduser-filter

Get-ADUser -Filter syntax: examples with -like, variables, dates and -Properties

The commands on this page have not been executed by our verification runner yet.updated
tool
Active Directory PowerShell
command
Get-ADUser
flag
-Filter
default
none; one of -Identity, -Filter or -LDAPFilter is required

Get-ADUser -Filter takes a query string in the Active Directory module's filter language, which looks like PowerShell: attribute -operator value, combined with -and, -or and -not. Put the whole filter in quotes and the value in inner quotes: Get-ADUser -Filter "Surname -like 'Smi*'". -Filter * returns every user. By default only a small set of properties is returned; add -Properties for anything else.

powershell
Import-Module ActiveDirectory
Get-ADUser -Filter "Surname -like 'Smi*'"
Get-ADUser -Filter 'Enabled -eq $false' | Select-Object Name, SamAccountName
Get-ADUser -Filter * -SearchBase 'OU=Sales,DC=contoso,DC=com' -Properties Department, Title

The module comes with RSAT (see install RSAT on Windows 11) and needs a domain controller with Active Directory Web Services.

Common filters

GoalFilter
Exact account name-Filter "SamAccountName -eq 'jdoe'"
Name starts with-Filter "Name -like 'Ann*'"
Disabled accounts-Filter 'Enabled -eq $false'
Has an email address-Filter 'EmailAddress -like "*"' -Properties EmailAddress
In a department-Filter "Department -eq 'Finance'" -Properties Department
Two conditions-Filter 'Enabled -eq $true -and Title -like "*manager*"' -Properties Title
UPN suffix-Filter "UserPrincipalName -like '*@contoso.com'"

Supported operators are -eq, -ne, -like, -notlike, -lt, -le, -gt, -ge, -approx, -bor, -band, -recursivematch, -and, -or and -not. PowerShell operators such as -match, -contains and -in are not part of the filter language; filter on the server with -like, then refine with Where-Object if needed.

Variables in a filter

The filter is a string that the module parses, so variables must end up as plain values:

powershell
$sam = 'jdoe'
Get-ADUser -Filter "SamAccountName -eq '$sam'"          # string: PowerShell expands $sam first

$user = Get-ADUser jdoe -Properties Department
$dept = $user.Department                                 # copy properties to a simple variable first
Get-ADUser -Filter 'Department -eq $dept' -Properties Department

Expressions such as $user.Department inside the filter are not evaluated by the module; assign them to a simple variable first.

Dates

powershell
$cutoff = (Get-Date).AddDays(-90)
Get-ADUser -Filter 'LastLogonDate -lt $cutoff -and Enabled -eq $true' -Properties LastLogonDate |
  Select-Object Name, LastLogonDate

LastLogonDate is a converted view of lastLogonTimestamp, which replicates with a delay of up to about two weeks; Search-ADAccount -AccountInactive wraps this query.

ParameterEffect
-SearchBase "OU=Sales,DC=contoso,DC=com"search only under this OU
-SearchScope Base | OneLevel | Subtreehow deep to search under SearchBase (default Subtree)
-Properties A, B (or *)return extra attributes; * returns all and is slow on large directories
-ResultSetSize Nstop after N results
-Server dc01.contoso.comquery a specific domain controller or another domain
-LDAPFilter "(mail=*@contoso.com)"use a raw LDAP filter instead of -Filter

Sources