Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active February 20, 2025 21:56
Show Gist options
  • Save jdhitsolutions/71efaf9df48cbfc0429ed6c1713e9cec to your computer and use it in GitHub Desktop.
Save jdhitsolutions/71efaf9df48cbfc0429ed6c1713e9cec to your computer and use it in GitHub Desktop.
A PowerShell script alternative to Compare-Object for comparing text files. This should be considered a proof-of-concept.
#requires -version 7.2
<#
this is a variation of what you could do comparing text files
using Compare-Object. The only difference really is how this
script formats the output.
This should be considered a PROOF-OF-CONCEPT
#>
Param(
[Parameter(
Position = 0,
Mandatory,
HelpMessage = 'Reference file to compare against'
)]
[ValidateScript({ Test-Path $_ }, ErrorMessage = 'File {0} cannot be found.')]
[Alias("rf")]
[string]$ReferenceFile,
[Parameter(
Position = 1,
Mandatory,
HelpMessage = 'The Difference file to compare'
)]
[ValidateScript({ Test-Path $_ }, ErrorMessage = 'File {0} cannot be found.')]
[Alias("df")]
[string]$DifferenceFile,
[Parameter(HelpMessage = 'Use a case-sensitive comparison')]
[Alias("cs")]
[Switch]$CaseSensitive
)
#a private helper function to display the differences using an ANSI sequence
Function HighlightDiff {
Param(
[string]$a,
[string]$b,
[switch]$CaseSensitive
)
#write-Host "Comparing $a to $b" -ForegroundColor Magenta
[regex]$rx = '\b\S+\b'
$aWords = $rx.Matches($a).Value
$bWords = $rx.Matches($b).Value
if ($aWords -AND $bWords) {
$differentWords = Compare-Object $aWords $bWords -CaseSensitive:$CaseSensitive |
Where-Object SideIndicator -EQ '=>' |
Select-Object -ExpandProperty InputObject
# Highlight different words in $b
$output = $b
foreach ($word in $differentWords) {
$output = $output -replace "\b$word\b", "`e[38;5;213m$word`e[0m"
}
$output
}
}
$ref = [System.Collections.Generic.List[string]](Get-Content $ReferenceFile)
$diff = [System.Collections.Generic.List[string]](Get-Content $DifferenceFile)
for ($i = 0; $i -le $ref.count; $i++) {
if ($CaseSensitive) {
$test = $diff[$i] -cne $ref[$i]
}
else {
$test = $diff[$i] -ne $ref[$i]
}
if ($test) {
[PSCustomObject]@{
PSTypeName = 'FileComparison'
Line = $i + 1
Ref = $ref[$i]
Comparison = '=>'
Diff = HighlightDiff -a $ref[$i] -b $diff[$i] -CaseSensitive:$CaseSensitive
}
}
}
if ($diff.count -gt $ref.count) {
#write-host "get extra lines from diff file" -ForegroundColor cyan
for ($i = $ref.count+1; $i -le $diff.count; $i++) {
[PSCustomObject]@{
PSTypeName = 'FileComparison'
Line = $i + 1
Ref = $Null
Comparison = '=>'
Diff = $diff[$i]
}
}
}
elseif ($ref.count -gt $diff.count) {
#write-host "get extra lines from ref file" -ForegroundColor green
for ($i = $diff.count+1; $i -le $ref.count; $i++) {
[PSCustomObject]@{
PSTypeName = 'FileComparison'
Line = $i + 1
Ref = $ref[$i]
Comparison = '<='
Diff = $Null
}
}
}
@jdhitsolutions
Copy link
Author

The difference is highlighted using an ANSI escape sequence.
image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment