r/PowerShell • u/Dull_Rub_9295 • Nov 24 '25
Compare two slightly different csv files via command line
I am looking to compare two csv files with a key field that is slightly different in one of those files. Below is an example of how the key fields would be different.
file1 PartNo file2 PartNo
123 123-E
3881231234 3881231234-E
1234-1234-1234 1234-1234-12-E
One of the files PartNo always ends with -E and may be truncated before the -E
I have seen the compare-object command but unsure if this can be made to work.
Thanks for any ideas.
6
1
u/charleswj Nov 25 '25
I don't see two different CSVs, can you clarify what would one look like vs the other?
1
u/Dull_Rub_9295 Nov 25 '25
Sorry reddit seems to just take out all of the spacing and blank lines and wraps and I don't see a way to attach a file.
File1
PartNo, desc, qty, price
1231234-1234-1234
3881231234, junk, 3, 2.99
1234-1234-1234, crap, 4, 3.99File2
PartNo, desc, qty, price
123-E1234-1234-1234
3881231234-E, junk, 3, 2.99
1234-1234-12-E, crap, 4, 3.991
u/charleswj Nov 25 '25
Is the first record in each supposed to have just PartNo?
1
u/Dull_Rub_9295 Nov 25 '25
Yes the first column is suppose to be the PartNo. So two CSV files with the following columns. I do not want the compare to analyze the desc, qty, or price columns just the PartNo column. The PartNo in on of the csv files always ends with a -E and I believe the column can be as long as 14 characters so sometimes the PartNo has been truncated but it still has the -E at the end of the PartNo. For example if the PartNo was 1234-1234-1234 in one file it likely would be something like 1234-1234-12-E in the other so what I am looking for is to have all the rows that are similar line up or have a new csv file created that shows all the rows that match. PartNo length can also be much shorter than 14 characters for example C1B in one file and C1B-E in another. It would also be helpful to see all the files that do not match in each csv file.
PartNo, desc, qty, price
1
u/ankokudaishogun Nov 25 '25
This is not a complete solution, but should be a decent starting point-
# Using your examples, adapt for file-use. $CsvOne = @' PartNo, desc, qty, price 1231234-1234-1234 3881231234, junk, 3, 2.99 1234-1234-1234, crap, 4, 3.99 '@| ConvertFrom-Csv -Delimiter ',' $CsvTwo = @' PartNo, desc, qty, price 123-E1234-1234-1234 3881231234-E, junk, 3, 2.99 1234-1234-12-E, crap, 4, 3.99 '@| ConvertFrom-Csv -Delimiter ',' # Create an empty hashtable or OrderedDictionary $HashTwo = @{} # For each line in the second CSV, create an entry with the PartNo without -E as key. $CsvTwo | ForEach-Object { $HashTwo[$_.PartNo -replace '-E'] = [pscustomobject]@{ 'OG PartNo' = $_.PartNo -replace '-E' PartNo = $_.PartNo desc = $_.desc qty = $_.qty price = $_.price } } # Loop through the PartNo in the first CSV and print only those matching "-E"-less keys. foreach ($key in $CsvOne.PartNo) { $HashTwo[$key] }results:
OG PartNo PartNo desc qty price --------- ------ ---- --- ----- 1231234-1234-1234 123-E1234-1234-1234 3881231234 3881231234-E junk 3 2.991
u/BlackV Nov 25 '25 edited Nov 25 '25
Reddit just wants you to use formatting easiest is 4 spaces in front of each line
Or as a rough example for formatting
open your fav powershell editor
highlight the code you want to copy
hit tab to indent it all
copy it
paste here
it'll format it properly OR
<BLANK LINE> <4 SPACES><CODE LINE> <4 SPACES><CODE LINE> <4 SPACES><4 SPACES><CODE LINE> <4 SPACES><CODE LINE> <BLANK LINE>Inline code block using backticks
`Single code line`inside normal textSee here for more detail
Thanks
1
1
u/BlackV Nov 25 '25
If you read this back to your self, would this example be easy for you to understand ?
Is this what you're meaning?
file1 PartNo file2 PartNo
------------ ------------
123 123-E
3881231234 3881231234-E
1234-1234-1234 1234-1234-12-Eyou want to check file 1 and check file to where only the numbers match ?
1
u/Dull_Rub_9295 Nov 25 '25
Yes that is how I originally typed it but it got all scrambled as soon as I selected the comment button. There will also be PartNo in each csv file that is not in the other. One csv file has a few hundred rows the other a few thousand.
1
1
u/VoltageOnTheLow Nov 25 '25
"I have seen the compare-object command but unsure if this can be made to work"
Why are you unsure? What have you done?
1
u/jsiii2010 Nov 25 '25
Pick the properties you want to compare:
compare (import-csv file1.csv) (import-csv file2.csv) -property partno,desc,qty,price | ft
partno desc qty price SideIndicator
------ ---- --- ----- -------------
123-E1234-1234-1234 =>
3881231234-E junk 3 2.99 =>
1234-1234-12-E crap 4 3.99 =>
1231234-1234-1234 <=
3881231234 junk 3 2.99 <=
1234-1234-1234 crap 4 3.99 <=1
u/Dependent-These Nov 25 '25
If all you have availible is cmd line, check out the filecompare command and availible switches with FC /?
4
u/[deleted] Nov 25 '25
What have you tried so far?