0

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?

Eric
  • 863
  • 4
  • 17
  • 31
  • 3
    Instead of `Select @{Expression={$_.InputObject}}` use `Select-Object -ExpandProperty InputObject` –  Nov 27 '18 at 15:57
  • 3
    `$File1 | Where {$File2 -Contains $_}`, see also: https://stackoverflow.com/a/35872835/1701026 – iRon Nov 27 '18 at 16:02

2 Answers2

4

You need to expand the InputObject property.

Compare-Object -ReferenceObject $File1 -DifferenceObject $File2 -IncludeEqual -ExcludeDifferent | 
    Select-Object -ExpandProperty InputObject | 
    Set-Content C:\Diff.txt
vrdse
  • 2,899
  • 10
  • 20
2

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

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

Razorfen
  • 423
  • 4
  • 12