Search code examples
powershellcomparison

PowerShell comparison outputs


I'm working on a Powershell script that creates a hash function when a new document is added to the folder and saved as a text file. I have two text files, and there are too many records inside. I want to compare the text documents and find the same values, but when I try to save new txt file, it is empty.

  • MD5 3F3CEC71FA0823D8E1BAA65A8E0D124F C:\Temp\Project\Backup\Archives\Batch\SB6_1-2-741245d_batch.pdf

  • MD5 C959765D30E703FA6B33E65541F50EEE C:\Temp\Project\Backup\Archives\Batch\SB6_1-2-743567422.xml

  • MD5 4865CA20AC06CFE22078BC8BE9E358D7 C:\Temp\Project\Backup\Archives\Batch\SB6_1-2-74794567standard_batch.bar

These are example values inside the txt file. I have the same records on the other txt file. I can compare the values, but when I save the new txt file it is empty. Here is my comparison script:

Compare-Object -ReferenceObject $(Get-Content

C:\Users\william\Documents\try1\a1.txt) -IncludeEqual $(Get-Content

C:\Users\william\Documents\try1\a2.txt)

Out-File C:\Users\william\Documents\try1\a3.txt -Encoding utf8

Solution

  • This is how I did it and it worked.

    $path = "C:\Users\william\Documents\try1"
    Compare-Object -ReferenceObject $(Get-Content $path\a1.txt) -IncludeEqual $(Get-Content $path\a2.txt) | Out-File $path\a3.txt -Encoding utf8