Powershell Compare Text Files and Show Differences

I need to do a file comparison and use Powershell. I need an output file with all the differences. The following is a good start, but I need a file to enter line numbers, and the resulting input object is now also disabled after 89 characters. I need a full line to display:

compare-object (get-content $File1) (get-content $File2) | Out-File $Location 
+11
source share
6 answers

The input object is truncated by the default display. To save the entire line in a file:

 compare-object (get-content $File1) (get-content $File2) | format-list | Out-File $Location 
+10
source
 $abc = gc .\z.txt | %{$i = 1} { new-object psobject -prop @{LineNum=$i;Text=$_}; $i++} $cde = gc .\x.txt | %{$i = 1} { new-object psobject -prop @{LineNum=$i;Text=$_}; $i++} Compare-Object $abc $cde -Property Text -PassThru -IncludeEqual 

Try this to spill out line numbers.

+3
source
 $apples = Get-Content D:\misc\1.txt $oranges = Get-Content D:\misc\2.txt Compare-Object -ReferenceObject $apples -DifferenceObject $oranges -PassThru | Out-File D:\misc\mm.csv 
+1
source

I used the following to save the entire line in a file

  Compare-Object -referenceObject $ (Get-Content $ File1) -differenceObject $ (Get-Content $ File2) |  % {$ _. Inputobject + $ _. SideIndicator} |  ft -auto |  out-file $ Location -width 5000
0
source
 $result= Compare-Object -ReferenceObject $(Get-Content D:\demo\misc\1.txt) -DifferenceObject $(Get-Content D:\demo\misc\1.txt) | Select -Property InputObject $result.InputObject 

Used above code to get 2 file difference

0
source

I used this answer https://serverfault.com/a/951843 . This gives a much more useful conclusion from my point of view:

  • Contains line numbers
  • Sorted by line numbers
  • Also uses powershell commands only.
0
source

Source: https://habr.com/ru/post/956085/


All Articles