Solved

Powershell - compare-object

Posted on 2011-09-28
3
748 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

695 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