Link to home
Create AccountLog in
Avatar of cawasaki
cawasaki

asked on

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
Avatar of oBdA
oBdA

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

Avatar of cawasaki

ASKER

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
test it gain, i dont now why every time it add this servername
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
hi,

the last one work :)
thanks