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](https://i0.wp.com/www.razumny.no/wp-content/uploads/image-114.png?resize=824%2C81&ssl=1)
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](https://i0.wp.com/www.razumny.no/wp-content/uploads/image-116.png?resize=789%2C84&ssl=1)
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"}
By posting a comment, you consent to our collecting the information you enter. See privacy policy for more information.