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?

2
  • 3
    Instead of Select @{Expression={$_.InputObject}} use Select-Object -ExpandProperty InputObject Commented Nov 27, 2018 at 15:57
  • 3
    $File1 | Where {$File2 -Contains $_}, see also: stackoverflow.com/a/35872835/1701026 Commented Nov 27, 2018 at 16:02

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.