I want to compare 2 text files and output the difference in another text file.
compare-object (get-content c:\temp\hostname_old.txt) (get-content c:\temp\hostname_new.txt) | Select-Object -ExpandProperty InputObject | Out-File $Location
hostname_old.txt
server02
server05
server04
server06
server01
hostname_new.txt (has duplicate names)
server04
server01
server02
server04
server02
Result:
server04
server02
server05
server06
Note how server04
and server02
are present in this list of differences, even though they're present in both input files.
This is what I want:
server05
server06
Use
Select-Object -Unique
to eliminate the duplicates before comparing:As in this answer to your previous question,
-PassThru
is used to pass out the differing lines directly, without the[pscustomobject]
wrappers (that indicate the source set of the difference via their.SideIndicator
property) thatCompare-Object
outputs by default.