搜索CSV和返回字段

时间:2016-02-16 14:56:58

标签: csv powershell

我有一个我想根据用户输入引用的csv文件。用户将键入员工的姓名,我希望它返回与该员工关联的薪资部门编号。我需要使用的3列标题为First,Last,Payroll Department Number。我做错了什么?

这就是我所拥有的:

$csv = import-csv c:\employees.csv
$First = read-host 'What is the users first name?'
$Last = read-host 'What is the users last name?'
$csv | where {$_.last -eq $Last; $_.'First Name' -eq $First} |
  % "Payroll Department Number"

1 个答案:

答案 0 :(得分:0)

使用Where-Object运算符替换-and条件中的分号,并将ForEach-Object语句替换为Select-Object

$csv | Where-Object { $_.last -eq $Last -and $_.'First Name' -eq $First } |
  Select-Object -Expand 'Payroll Department Number'