0

I have two files/outputs. $file1 contains:

Name,Address,Number

$file2 contains:

Name

I'm looking to match those two files on Name and output the entire line from $file1

Matty T
  • 3
  • 3
  • Using this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)): `Import-Csv .\file1.csv |Join-Object (Import-Csv .\file2.csv) -On Name` – iRon Jun 18 '22 at 05:28

1 Answers1

1

Assuming that your files are CSV files:

Combine Import-Csv with Where-Object to filter those lines, with the help of the
-inoperator:

Import-Csv $file1 | # Parse the CSV file into objects
  Where-Object Name -in (Import-Csv $file2).Name # Filter

Note:

  • This is a conceptually simple, but potentially slow solution, depending on the size of the input files. More efficient - but more elaborate - solutions are possible.

  • The filtered CSV rows are output as objects (of type [pscustomobject]), as parsed by Import-Csv; you could convert them back to CSV via ConvertTo-Csv (in memory) or save them back to a file with Export-Csv.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Thank you very much! that got my brain juices flowing haha. In the end, i ended up figuring it out and did this: `$output = $file2 | Where-Object Name -in $file1` and then `$output | Export-csv -path pathtofile` – Matty T Jun 24 '22 at 00:03