Steve
Steve

Reputation: 137

PowerShell txt file comparison not finding matches

My PowerShell adventures continue… After finally figuring out file manipulation and some regular expressions to format my text, I finally have 2 text files which I need to compare.

Compare-Object $(Get-Content C:\File1.txt) $(Get-Content C:\File2.txt) -includeequal > C:\Results.txt

Problem: When using Compare-Object and Get-Content on 2 separate text files the comparison results show no matches. I know for a fact this is incorrect as I can manually find strings identical to both. The text is a random ID number structured like so:

GTD-LVOE-FS-0032

Each text file itself may contain multiple copies for a particular ID number and File2 is a master list containing all occurrences of the ID numbers.

Solution: I expect Compare-Object with the –includeequal to return some == results as I know this is the case. Are there formatting issues I may be overlooking that are causing this or does Compare-Object not work like I am expecting when multiple identical strings exist within and between files?

EDIT:

As a test I created 2 text files each containing the following.

GTD-LVOE-43-0021
GTD-LVOE-43-0021
GTD-LVOE-43-0021
GTD-LVOE-43-0021

I expect Compare-Object to say these text files are identical, rather it returns that File2 contains the strings. It seems to me the issue is with Compare-Object handling identical strings within one of the variables created for the compare. Is there another CMDLET or method to use for comparing objects when there are multiple identical strings within the variables that need to be compared?

Upvotes: 1

Views: 1550

Answers (1)

Shay Levy
Shay Levy

Reputation: 126762

Works for me (based on the content you posted, same content for file1 and file2):

PS> compare (gc File1.txt) (gc File2.txt) -IncludeEqual | ft -a

InputObject      SideIndicator
-----------      -------------
GTD-LVOE-43-0021 ==
GTD-LVOE-43-0021 ==
GTD-LVOE-43-0021 ==
GTD-LVOE-43-0021 ==

Upvotes: 1

Related Questions