PowerTip: Compare the Contents of Files with PowerShell

PowerTip: Compare the Contents of Files with PowerShell

  • Comments 11
  • 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
    }
    }

  • Hi there,
    very useful!
    Thanks for the sweet script Vantienen Stef.

    I don't understand how this particular operation works out. Can someone kindly explain it?
    Line 18: .. $objnotinfile2="" +="">
    cheers Snerin

  • @snerin,

    i think he typed it wrong, instead of $objnotinfile2="" +="">, it should be $objnotinfile2+=$_

  • This is great for comparing differences after a GPO update. Run:

    gpresult /x "_before.xml"
    gpupdate /force
    gpresult /x "_after.xml"

    Then:
    compare (Get-Content "_before.xml") (Get-Content "_after.xml")

    This command is also very helpful when trying to install Windows feature sets on a new server. For example, while configuring a new Lync Director, I ran:

    compare (Get-WindowsFeature | where InstallState -eq Installed).Name (Get-WindowsFeature -ComputerName fe01 | where InstallState -eq Installed).Name

    This lists the differences between the installed features on each server. Because I still needed to install everything listed from the Front End server, I ran:

    (Get-WindowsFeature -ComputerName fe01 | where InstallState -eq Installed) | Add-WindowsFeature

    The guide I was going off of didn't provide a PowerShell command to copy/paste, so this saved some time. Be sure to include the "where InstallState -eq Installed" part or it grabs ALL features and tries to install them!

  • You could also simplify the entire operation of comparing Group Policy updates by directly comparing variables.

    $before = (gpresult /R /V); gpupdate /force; $after = (gpresult /R /V); Compare-Object $before $after

    Now you're typing one line and getting your answers.

  • If anyone is still interested, I got Vantienen Stef's script to run after making a few edits.

    cls
    $objFile1 = Get-Content "C:\Users\bb\Desktop\Output1.csv"
    $objFile2 = Get-Content "C:\Users\bb\Desktop\Output2.csv"

    $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
    }
    }

  • Can some one help me on how to get it export the result in .csv file for Vantienen Stef script.