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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...