Powershell Compare-Object Output to File

Powershell Compare-Object Output to File

I have 2 simple text files that I need to compare and output the items that are in BOTH files

Archive.TXT

10000
10001
10002
10003
10005
10010
10011

Active-Job-List.TXT

10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012

This will give me an output

$File1 = Get-Content C:\Archive-List.txt
$File2 = Get-Content C:\Active-Job-List.txt



   Compare-Object -ReferenceObject $File1 -DifferenceObject $File2 -IncludeEqual -ExcludeDifferent | Select @{Expression={$_.InputObject}} | Set-Content C:\Diff.txt

of this in the Diff.txt

@{$_.InputObject=10000}
@{$_.InputObject=10001}
@{$_.InputObject=10002}
@{$_.InputObject=10003}
@{$_.InputObject=10005}
@{$_.InputObject=10010}
@{$_.InputObject=10011}

But what I am really looking for is an output of this

10000
10001
10002
10003
10005
10010
10011

How can I filter the results to what I want? Should I Get-Content of the Diff.txt and "trim" out things I don't want or is there a simpler way?

2

2 Answers

You need to expand the InputObject property.

Compare-Object -ReferenceObject $File1 -DifferenceObject $File2 -IncludeEqual -ExcludeDifferent | 
    Select-Object -ExpandProperty InputObject | 
    Set-Content C:\Diff.txt

You could add a -PassThru to your command like so:

Compare-Object -ReferenceObject $File1 -DifferenceObject $File2 -IncludeEqual -ExcludeDifferent -PassThru | Set-Content C:\Diff.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.