Solved

Powershell - compare-object

Posted on 2011-09-28
3
743 Views
Last Modified: 2012-05-12

I am trying to use Powershell to compare two files and save the results into a file.
This is working ok. However I would like the output to include the line numbers.

I saw one forum suggest this could be done by comparing array, looking for some other feedback / examples.

$mydiff = Compare-Object $(Get-Content $myfile1) $(Get-Content $myfile2) | Out-String

Thank you,
Mark
0
Comment
Question by:markpalinux
3 Comments
 
LVL 18

Expert Comment

by:x-men
ID: 36813954
something like:

compare-object $(get-content .\test1.txt) $(get-content .\test2.txt) | % {SELECT-string -path .\test1.txt -pattern $_.InputObject }
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 36931416
compare-object is not able to do that. And as long as your lines are not unique, you cannot combine the result of it with the files to get the line number. You will need to write your own compare cmdlet for that, comparing two string arrays line by line.
IMHO it is much easier to use any of the existing external tools, like diff.exe, to do the comparision.
0
 
LVL 15

Author Closing Comment

by:markpalinux
ID: 37111992
I had Compare-IT , really great tool. It had command line support. With this I sent powershell to grab the file paths and let compare it create html files with the diff results.

Thanks.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
A procedure for exporting installed hotfix details of remote computers using powershell
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question