Solved

Powershell - compare-object

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

740 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