Solved

Powershell - compare-object

Posted on 2011-09-28
3
746 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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