loi219
loi219

Reputation: 248

Import-CSV and Get-ADuser are returning nothing

The command line is returning nothing when I'm using it.
I've already changed Name to DisplayName or CN but this wasn't working too.

Import-Csv "D:\Temp\userExcelRights.txt" |
  %{ Get-ADUser -Filter {Name -eq "*$_*"} } |
  Select Name,Enabled,SamAccountName,DistinguishedName

The CSV file contains the full name of users.

Upvotes: 0

Views: 318

Answers (2)

loi219
loi219

Reputation: 248

With this command it's working:

$data=@(Import-Csv "D:\Temp\userExcelRights.txt").Name 
$data | %{ Get-ADUser -Filter "name -like '*$_*'" } |
  Select Name,Enabled,SamAccountName,DistinguishedName

Thanks to Ansgar Wiechers.

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

The -eq operator doesn't support wildcard matches, you need to use the -like operator for that, and you must select a property from the objects that Import-Csv procuces. Also, avoid scriptblock syntax for Get-ADUser filters. Replace

{Name -eq "*$_*"}

with

"Name -like '*$($_.Name)*'"

If the subexpression doesn't work for you, you may need to expand the Name property before feeding it into the loop:

Import-Csv 'D:\Temp\userExcelRights.txt' |
  select -Expand Name |
  %{ Get-ADUser -Filter "Name -eq '*$_*'" } |
  ...

Upvotes: 1

Related Questions