PowerTip: Compare the Contents of Files with PowerShell

PowerTip: Compare the Contents of Files with PowerShell

  • Comments 5
  • Likes

Summary: Learn how to use Windows PowerShell to compare the contents of two file.

Hey, Scripting Guy! Question How can I use Windows PowerShell to compare the contents of two files?

Hey, Scripting Guy! Answer 

  • If the files are text files, use Compare-Object:

Compare-Object -ReferenceObject (Get-Content .\diskcapacity.csv) -DifferenceObject
(Get-Content .\diskcapacity2.csv)

Note  You have to get the content of the two files and perform the comparison on the two resultant objects. If you use only the file names, you will get a comparison of the file objects rather than the content.

  • If there are no differences between the contents of the two files, you won’t see any output. Test what is happening by using the –IncludeEqual parameter:

Compare-Object -ReferenceObject (Get-Content .\diskcapacity.csv) -DifferenceObject
(Get-Content .\diskcapacity2.csv) -IncludeEqual

This outputs each line that matches with an equality indicator. You will get a lot of output if the files are large! 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks

    always interesting

  • Compare-Object is very useful command - Helped me in many ways :)

  • I've used Compare-Object in the following way to determine the differences between both files

    Compare-Object (Get-Content $file1) (Get-Content $file2) | where {$_.SideIndicator -eq "<="}

  • Good android app to compare files and folders https://play.google.com/store/apps/details?id=com.ravindra.filecompare.pro&hl=en

  • #-- a more graphical solution (with rownumbers and highlights) can be retrieved with next code
    cls
    $objFile1 = Get-Content "C:\Data\file1.txt"
    $objFile2 = Get-Content "C:\Data\file2.txt"

    $objFile3 = $objFile1 | %{$i = 1} { new-object psobject -prop @{LineNum=$i;Text=$_}; $i++}
    $objFile4 = $objFile2 | %{$i = 1} { new-object psobject -prop @{LineNum=$i;Text=$_}; $i++}

    $objDiff1 = Compare-Object -ReferenceObject $objFile1 -DifferenceObject $objFile2
    $objNotInFile1 = @()
    $objNotInFile2 = @()
    $objDiff1 | foreach {
    if ($_.SideIndicator -eq "=>") {$objNotInFile1 += $_}
    if ($_.SideIndicator -eq "<=") {$objnotinfile2="" +="">
    }
    $objDiff2 = Compare-Object -ReferenceObject $objFile3 -DifferenceObject $objFile4 -IncludeEqual -Property Text,LineNum | sort-object -Property LineNum

    $lngCounterNotInFile1 = 0
    $lngCounterNotInFile2 = 0
    $lngMaxCharacters = 60
    foreach ($objDifference in $objDiff2) {
    $blnIsDifference = $false
    if ($objDifference.SideIndicator -eq "=>") {
    if ($objNotInFile1[$lngCounterNotInFile1].InputObject -eq $objDifference.Text){
    "{0}[{1}]`t{2}" -f $objDifference.SideIndicator, $objDifference.LineNum, $objDifference.Text | Write-Host -BackgroundColor yellow -ForegroundColor black
    if ($lngCounterNotInFile1 -lt $objNotInFile1.count) {$lngCounterNotInFile1 ++}
    $blnIsDifference = $true
    }
    }elseif ($objDifference.SideIndicator -eq "<=")>
    if ($objNotInFile2[$lngCounterNotInFile2].InputObject -eq $objDifference.Text){
    "{0}[{1}]`t{2}" -f $objDifference.SideIndicator, $objDifference.LineNum, $objDifference.Text | Write-Host -BackgroundColor yellow -ForegroundColor black
    if ($lngCounterNotInFile2 -lt $objNotInFile2.count) {$lngCounterNotInFile2 ++}
    $blnIsDifference = $true
    }
    }
    if ($blnIsDifference -eq $false) {
    $strLine = $objDifference.Text
    $strLine = "$strLine".replace("`t"," ")
    if ("$strLine".length -gt $lngMaxCharacters){$strLine = "$strLine".substring(0,$lngMaxCharacters-3)+"..."}
    "[{0}]`t{1}" -f $objDifference.LineNum, $strLine | Write-Output
    }
    }