Skip to main content

Comparing Text Files with PowerShell

Being a Microsoft Certified Trainer, I work with a lot of different clients spread over 11 different time zones.  I sometimes need a little help when it comes to looking at contracts from my clients.  The old saying “All is fair in love and business” is something that all business owners needs to remember.  Fortunately for me, from client to client, contracts are fairly identical.  I still like to look for anything that has changed from a previous contract with a client, but this can be time consuming. 

Below is a little script that I use to highlight any changes.  I just simply save the contents of the contracts into a text file and feed the new contract and the previous one into this script.  This script will highlight any differences in each line.  This helps to quickly direct my attention where it needs to be.  In most cases, the contracts are 99% identical from one to the other.  This just helps to make sure that I do not miss that 1%.

One thing to note is the use of Write-Host in this code.  The design of the code is note to be used in the PowerShell pipeline.  It is to be displayed utilizing colors.

Function Compare-Contract

{

[cmdletBinding()]

Param (

    $Previous = "C:\Users\JASON\Documents\PowerShell\InDevelopment\CompareContract\Previous.txt",

    $Current = "C:\Users\JASON\Documents\PowerShell\InDevelopment\CompareContract\Current.txt"

)

 

 

    # Function: Write-Line

    # Writes the characters out one by one.  Green if they match, red if they do not.

    # The LoneLine and ShortLine allows for any lines that have extra

    # Characters in them to be marked in red.

    Function Write-Line

    {

        Param ($LongLine, $ShortLine)

        $Y = $LongLine.Count

 

        Try

        {

            For ($X = 0;$X -lt $Y; $X++)

            {

                If($LongLine[$X] -eq $ShortLine[$X])

                {

                    Write-Host "$($LongLine[$X])" -NoNewline @Color0

                }

                Else

                {

                    Write-Host "$($LongLine[$X])" -NoNewline @Color1

                }

               

            }

        }

        Catch

        {Write-Host "$LongLine[$X]" -NoNewline @Color1}

 

        # Adds a carriage return to the line.

        Write-Host ""

 

    } # End: Function Write-Line

 

    # Read the content of the text files.

    $Previous = Get-Content $Previous

    $Current = Get-Content $Current

 

    # Split the contracts on each line.  This helps to reduce

    # the false positives.

    $Prev = $Previous.Split("`n")

    $Curr = $Current.Split("`n")

   

    # Color splats used to help clarify what I need to look at.

    $Color0 = @{ForegroundColor="Green";BackgroundColor = "DarkBlue"}

    $Color1 = @{ForegroundColor="Red";BackgroundColor = "DarkRed"}

    $Color2 = @{ForegroundColor="Cyan";BackgroundColor = "DarkBlue"}

    $Color3 = @{ForegroundColor="Magenta";BackgroundColor = "DarkBlue"}   

    

    # Lets you know if any lines were added or removed between contracts.

    Write-Host "Previous contract line count: $($Prev.Count)" @Color2

    Write-Host "Current contract line count: $($Curr.Count)" @Color2

    Write-Host "-------------------------------------------------------"

 

    # Find out what the maximum line count is between the two contracts.

    $Y = $Prev.count, $Curr.count | Measure-Object -Maximum |

         Select-Object -ExpandProperty Maximum

 

    # Loop through the contracts line by line.

    # The IF Statements makes sure that if one contract is longer then

    # the other, that the extended text will be marked red.

    For ($X = 0; $X -lt $Y; $X++)

    {

 

        Try

        {

            If ($Curr.count -ge $Prev.count)

            {

                Write-Host "[$($X)]" -NoNewLine @Color3

                $P = $Prev[$X].ToCharArray()

                $C = $Curr[$X].ToCharArray()

                Write-Line -LongLine $C -ShortLine $P

            }

        }

        Catch

        {

            Write-Host $Curr[$X] @Color1

        }

           

        Try

        {   

            If ($Curr.count -lt $Prev.count)

            {

                Write-Host "[$($X)]" -NoNewLine @Color3

                $P = $Prev[$X].ToCharArray()

                $C = $Curr[$X].ToCharArray()

                Write-Line -LongLine $P -ShortLine $C

            }

        }

        Catch

        {

            Write-Host $Prev[$X] @Color1

        }

 

 

    } # End: For ($X = 0; $X -lt $Y; $X++)

<#

.SYNOPSIS

Compares two sets of texts for differences.

 

.DESCRIPTION

Compares two sets of texts for differences.  Highlights lines with

different characters in red.

 

.PARAMETER Previous

The text file of the previous contract.

 

.PARAMETER Current

The text file of the current contract.

 

.EXAMPLE

Compare-Contract -Previous Previous.txt -Current Current.txt

 

.NOTE

Output is written to the host utilizing Write-Host.  Output to an object

is not part of this codes design.

 

#>

} # End: Compare-Contract

 

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.