PowerTip: Compare the Contents of Files with PowerShell


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! 

Comments (14)

  1. Anonymous says:

    @Ariff – Simple you just need to add 3 lines…

    Line 24 – "{0}[{1}]`t{2}" -f $objDifference.SideIndicator, $objDifference.LineNum, $objDifference.Text | Out-File ENTER LOCATION HERE.csv -Append

    Line 31 – "{0}[{1}]`t{2}" -f $objDifference.SideIndicator, $objDifference.LineNum, $objDifference.Text| Out-File ENTER LOCATION HERE.csv -Append

    Line 41 – "[{0}]`t{1}" -f $objDifference.LineNum, $strLine | Out-File ENTER LOCATION HERE.csv -Append

    Joe

  2. Powersell says:

    Thanks

    always interesting

  3. Chen V says:

    Compare-Object is very useful command – Helped me in many ways 🙂

  4. Maffiow says:

    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 "<="}

  5. Vantienen Stef says:

    #– a more graphical solution (with rownumbers and highlights) can be retrieved with next code
    cls
    $objFile1 = Get-Content “C:Datafile1.txt”
    $objFile2 = Get-Content “C:Datafile2.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
    }
    }

  6. Snerin says:

    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

  7. NOEL says:

    @snerin,

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

  8. Jon says:

    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!

  9. N.Olsen says:

    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.

  10. bb says:

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

    cls
    $objFile1 = Get-Content "C:UsersbbDesktopOutput1.csv"
    $objFile2 = Get-Content "C:UsersbbDesktopOutput2.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
    }
    }

  11. Ariff says:

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

  12. AC says:

    BB, thanks for the fix. When comparing two text files, where the first (file1) is the original, and the second (file2) is the one being compared….If on file 2 a line or two are removed, then the resultant comparison file gets a little messy duplicating
    line numbers (sort of like lines being shifted). Is there anything anyone can help to modify the script?

  13. Learner says:

    Very nice script, can u please add another function to outstream the only differences to text, XML file.

Skip to main content