PowerShell 2 min read
How to Search Users Across Active Directory Domains in PowerShell (Global Search)
Michael Wu ·
In large organizations with multiple domains, administrators need to identify users who have been moved across domains. This article explains two approaches.
Active Directory Infrastructure
The example uses a forest with three domain controllers:
- DC1.mike.com — Primary domain controller in mike.com domain
- DC2.child.mike.com — Global Catalog DC in child domain
- DC3.child.mike.com — Domain controller in child domain
Method 1: Active Directory Administrative Center (ADAC)
For single-user searches, ADAC provides the fastest approach:
- Select “Global Catalog Search” in the GLOBAL SEARCH scope
- Type the user’s name in the search box
- Results use the ANR (Ambiguous Name Resolution) LDAP filter
Method 2: PowerShell Script
For batch operations, a PowerShell script is more efficient:
# Check samaccountname and list users not in the domain
$filepath = "C:\temp\shares"
$allfolders = Get-ChildItem -Directory -Path $filepath
$obj2 = @()
foreach ($folder in $allfolders) {
try {
if (Get-ADUser $folder.name -ErrorAction Stop) {
$AD = "Y"
}
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
$AD = "N"
$displayname = Get-ADObject -LDAPFilter "samaccountname=$($folder.name)" `
-Server "dc1.mike.com:3268" -Properties * |
Select-Object -ExpandProperty displayname
$DistinguishedName = Get-ADObject -LDAPFilter "samaccountname=$($folder.name)" `
-Server "dc1.mike.com:3268" -Properties * |
Select-Object -ExpandProperty DistinguishedName
$obj = [pscustomobject]@{
ID = $folder.Name
Name = $displayname
DistinguishedName = $DistinguishedName
}
$obj2 += $obj
}
}
$obj2 | Format-Table
# Export to CSV
$csvpath = "c:\temp\export.csv"
$obj2 | Export-Csv -Path $csvpath -NoTypeInformation
Key Details
- Line 11: Checks whether the user exists in the current domain using
Get-ADUser - Lines 17–18: For users not found, performs a global search
- Port 3268: The Global Catalog query uses this specific port (
dc1.mike.com:3268), enabling cross-domain search functionality