Solved

Powershell - compare-object

Posted on 2011-09-28
3
731 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
Comment Utility
something like:

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

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Folder Permission Powershell 4 33
Powershell: Try/Catch help 2 22
Exchange Powershell help 6 29
Powershell String Parsing 3 16
Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now