Match the closest similarity

Keeganz 1 Reputation point
2022-02-25T06:13:45.237+00:00

Hi all,

If the exported csv file is missing the first name for the managerfullname column, how can I modify the below command such that the system will still be able to match the correct Manager's Displayname on AD.

     $manager = (Get-ADUser -Filter "displayname -eq '$($_.ManagerFullName)'").distinguishedname  

Exported Csv File
UserEmail ManagerFullName
test@keyman .com Lim Wei Xing ----------------> missing first name Daniel

Display name for Manager on AD : Daniel Lim Wei Xing

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,530 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DaveK 1,856 Reputation points
    2022-03-08T10:22:19.76+00:00

    Hi, This should get your started.

    Import-Csv -Path .\names.csv | ForEach {
        $Filter = "displayname -like $([char]34+[char]42+$($_.ManagerFullName)+[char]42+[char]34)"
        Get-Aduser -Filter $Filter
    }
    

    The filter is built up separate to capture the ManagerFullName with " before and after the name, and to include * before and after to allow it to search for all - "searching_for"
    Someone else might be able to suggest a cleaner way to formulate the Filter but I started to do it using [Char] as this generally gives me less trouble when formulating strings with lots of " or ' within them.

    As this search is based on partial match, depending on your AD size you may get multiple results for common surnames. You don't mention it but if you always have access to the email address in the CSV, would searching against that be better as its likely to give unique results?

    0 comments No comments

  2. Rich Matheisen 46,796 Reputation points
    2022-03-08T15:48:54.733+00:00

    The answer will depend on how loose, or approximate, you want the match to be.

    If you're not concerned with finding multiple matches you can just use the "-like" operator instead of the "-eq" operator. Be sure to use a leading asterisk (wildcard) in the resulting string.

    $manager = (Get-ADUser -Filter "displayname -like '$('*' + $_.ManagerFullName)'").distinguishedname
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.