compare 2 csv file

cawasaki
cawasaki used Ask the Experts™
on
i need to compare 2 csv file contain list of server:
the first file contain:

server1
server2
server3

the second file:

server0
server30
server1
server3
server2
server40

i need to compare this 2 csv file and only export the difference, the result will be:

server0
server30
server40

thanks for help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
This gets only names that are in the second file, but not in the first.
If you want names as well that appear only in the first file, but not in the second, just remove the line "Where-Object ..."
$outFile = 'C:\Temp\diff.csv'
$reference = Get-Content -Path 'C:\Temp\Servers1.csv'
$difference = Get-Content -Path 'C:\Temp\Servers2.csv'
Compare-Object -ReferenceObject $reference -DifferenceObject $difference |
	Where-Object {$_.SideIndicator -eq '=>'} |
	Select-Object -ExpandProperty InputObject |
	Set-Content -Path $outFile

Open in new window

Author

Commented:
hello oBdA

Test it but the result is not correct, i have get the 2 line not present in first csv plus other line present in the 2 csv file

Author

Commented:
test it gain, i dont now why every time it add this servername
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Can't reproduce; you need to provide more details than "every time it add this servername".
This is what happens with the samples you provided:
PS C:\> Get-Content -Path 'C:\Temp\Servers1.csv'
server1
server2
server3
PS C:\> Get-Content -Path 'C:\Temp\Servers2.csv'
server0
server30
server1
server3
server2
server40
PS C:\> Compare-Object -ReferenceObject $reference -DifferenceObject $difference |
>> Where-Object {$_.SideIndicator -eq '=>'} |
>> Select-Object -ExpandProperty InputObject
server0
server30
server40
PS C:\>

Open in new window


Maybe the old issue of a trailing space?
$outFile = 'C:\Temp\diff.csv'
$reference = Get-Content -Path 'C:\Temp\Servers1.csv' | ForEach-Object {$_.Trim()}
$difference = Get-Content -Path 'C:\Temp\Servers2.csv' | ForEach-Object {$_.Trim()}
Compare-Object -ReferenceObject $reference -DifferenceObject $difference |
	Where-Object {$_.SideIndicator -eq '=>'} |
	Select-Object -ExpandProperty InputObject |
	Set-Content -Path $outFile

Open in new window

Author

Commented:
hi,

the last one work :)
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial