compare 2 csv file

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
cawasakiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
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

1
cawasakiAuthor 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
0
cawasakiAuthor Commented:
test it gain, i dont now why every time it add this servername
0
oBdACommented:
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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cawasakiAuthor Commented:
hi,

the last one work :)
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.