compare text files and export result

Thor El Poderoso 51 Reputation points
2021-08-19T14:29:37.753+00:00

I need you to help me study to propose a script that compares 2 text files and creates one for me with the same strings. How would you do it? I just need advice.

file1.txt
C:\tmp\1\11111.mp3
C:\tmp\1\11112.mp3
C:\tmp\3\11113.mp3

file2.txt
C:\tmp\1\11111.mp3
C:\tmp\2\11112.mp3
C:\tmp\3\11113.mp3

result.txt
C:\tmp\1\11111.mp3
C:\tmp\3\11113.mp3

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,123 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
867 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-08-19T14:50:44.487+00:00

    Use Compare-Object:

    (Compare-Object -ReferenceObject (Get-Content c:\junk\file1.txt) -DifferenceObject (Get-Content c:\junk\file2.txt) -IncludeEqual -ExcludeDifferent).InputObject |
        Out-File c:\junk\resultfile.txt
    
    0 comments No comments