Get-AdUser Lookup for each user in csv list with LDAPFilter anr match

  active-directory, get-aduser, powershell, windows

I’m querying AD for user details using a list of usernames derived from a different list meaning that not all usernames match the SamAccountName exactly e.g. might have a number or letter dropped from the end. I can get the exact match lookup to work and output the names it can’t find but I’d like to take that list names and run them through an LDAPFilter anr search to check for fuzzy matches as well. So far I have:

ForEach($User in $List){
Write-host "Now checking $User"

Try{
     Get-ADUser -LDAPFilter "(anr=$User)" -Properties * | 
     select-object DisplayName,UserPrincipalName,mail,Enabled | 
     Export-CSV -Append $OutputFileResults -NoTypeInformation
     Write-host "$User found successfully" -foregroundcolor Green
     }
Catch{
    $User | Export-CSV -Append $OutputFileFailed -NoTypeInformation
    Write-host "$User not found" -foregroundcolor Red
    }
}

At the moment the output I get just says that the username was found successfully but writes nothing into the output file.

Source: Windows Questions

LEAVE A COMMENT