Powershell find users with similar family names

Hi,

I have a script that searches for users that have the exact Firstname I provide and Surnames that are alomst identical
So, when I provide the script the firstname "Sara" and surname "Smith", I want it to find al users that have names like:

Sara Smith
Sara Smithonian
Sara Smith Klein etc.

I'm using this line:
$names = $(try {Get-Aduser -filter * -Properties Surname | Where {$_.Givenname -like "$Givenname" -and $_.Surname -like "*$Lastname*"}} catch {$null})

I kind of works. It just finds only users that have the exact First and Surname I provide the script.
So, in the example above, it only finds all the "Sara Smiths" in AD, but not the Sara's with similar familynames.

How can I get this done ?
Thanks in advance.
LVL 2
LoyallAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy LidbetterCommented:
First off, it's more efficient to use the filter than to get everything and then find what you are looking for, also the surname field is canned so does not need to be included.

Ok, now this is kinda weird... It doesn't like the quotation marks... I got around it like this...

$GivenName = "Sarah"
$LastName = "*Smith*"
$names = Get-Aduser -filter {(givenName -like $Givenname) -and (surname -like $Lastname)}

Open in new window


If the Surname is a user input then I converted it like this

$Name = Read-host
$GivenName = "*" + $Name + "*"

Open in new window


And it seems to work....

Or I suppose when you feed the script a name... include the * in the input.. That would work too...
1
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Variable substitution in expressions inside of -filter only works if you provide the filter as string, not as scriptblock:
$names = Get-ADUser -filter "(givenName -eq $GivenName) -and (surname -like '*$LastName*')"

Open in new window

and having a filter is definitely the better way to start a search.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LoyallAuthor Commented:
I'm very sorry for my delayed response. I will test it tomorrow.
0
LoyallAuthor Commented:
My question has been answered
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.