No result with CSV imported parameters from Get-aduser command

I am trying to use LastName and Firstname parameters to pull SamAccountName for users. I have created a csv file with all the names and imported to powershell. The import works ok, but following command doesn’t yield any results. No errors either.

$c | ForEach-Object{ Get-aduser -filter "Surname -like '*$_.LastName*'" | select SamAccountName}

First time posting on this forum. I welcome any feedback to make my question more clear.

Randeep4531,
Welcome to the forum. :wave:t3:

It seems like you’re using only the lastname. :point_up: :wink:

Are you unsure about the names or why do you use asterisks?

I’d actually use something like this:

$C  | 
ForEach-Object{ 
    Get-ADUser -Filter "Surname -eq '$($_.LastName)'"} | 
      Select-Object -Property SamAccountName

If you insist on using asterisks it should look like this:

$C  | 
ForEach-Object{ 
    Get-ADUser -Filter "Surname -like '*$($_.LastName)*'"} | 
      Select-Object -Property SamAccountName

The $(...) is called subexpression operator and here you can read more about:

In general:
Please do not post images of code or console output. Instead post the plain text and format it as code.
And I’d recommend to format your code nicely. It makes it easier to read and easier to understand.

Thanks in advance

And BTW:
If you want to use first AND last name the code should look like this:

$C  | 
ForEach-Object{ 
    Get-ADUser -Filter "Surname -eq '$($_.LastName)' -and GivenName -eq '$($_.FirstName)'"} | 
      Select-Object -Property SamAccountName
1 Like

Thank you @Olaf that worked. I’ll check out subexpressions and thanks for your feedback on postings.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.