PowerShell: Find User based on partial name

Some time ago, I needed to look up the account names of a handful of users in Active Directory. The information I had beforehand was their names. I did not have ADUC available to me, but I did have PowerShell. This is an excellent use case for the Get-ADUser cmdlet when combined with the -Filter parameter. Here’s what my first stab at the problem looked like this:

Get-ADUser - Filter displayName "Givenname Surname"

That, however, threw an error message:

Get-ADUser : A positional parameter cannot be found that accepts argument 'Firstname Surname'.
At line:1 char:1
+ Get-ADUser -Filter displayName "Firstname Surname"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser

I next added a comparison operator to my command, as follows:

Get-ADUser -Filter displayName -like "Firstname Surname"

This, too, threw an error message:

Get-ADUser : A parameter cannot be found that matches parameter name 'like'.
At line:1 char:32
+ Get-ADUser -Filter displayName -like "Firstname Surname"
+                                ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Once I added curly brackets to the command, however, did get the result I wanted – although not for all the users. Whenever it didn’t find a user, it didn’t return a result. On a hunch that it might be down to spelling inconsistencies, I removed everything but their surname, and added a wildcard, which finally netted me all the users’ account names. Here is my final query:

Get-ADUser -Filter {displayName -like "*Surname"}


Posted

in

,

by

Tags:

Comments

By posting a comment, you consent to our collecting the information you enter. See privacy policy for more information.

This site uses Akismet to reduce spam. Learn how your comment data is processed.