Powershell : Compare two file interactively

I you need to compare two similar files then I though it would be a good idea to put together a quick script that can do this for you, with an interactive menu so we can reverse the logic.

This script will look in the current directory for, in this example text files, and display them as below with a menu where you select the first file:



Then you select the second file:



You will then be advised what files you have selected from here we can do the compare:


In this example we will choose option 1 which will look for differences between "fruits.txt" and "onemissing.txt" (of which Blueberry's is definitely missing)


This is correctly reported by the script, obviously this is a simple example but if you have for example a list of OU in the Distinguished Name (DN) format then this can easily tell you what is missing from those two files.

Script : CompareFilesMenu.ps1

function Show-FileContents {
    param (
        [string]$filePath,
        [int]$previewLines = 3
    )
    
    try {
        $content = Get-Content -Path $filePath -TotalCount $previewLines -ErrorAction Stop
        Write-Host "`nFirst $previewLines lines of '$($filePath)':" -ForegroundColor Cyan
        Write-Host "----------------------------------------"
        $content | ForEach-Object { Write-Host $_ }
    }
    catch {
        Write-Host "Could not read '$filePath'" -ForegroundColor Red
        return $false
    }
    return $true
}

function Compare-Files {
    param (
        [string]$sourceFile,
        [string]$targetFile
    )
    
    $sourceEntries = Get-Content -Path $sourceFile
    $targetEntries = Get-Content -Path $targetFile
    
    # Convert target entries to hashtable for faster lookup
    $targetHash = @{}
    foreach ($entry in $targetEntries) {
        if (-not [string]::IsNullOrWhiteSpace($entry)) {
            $targetHash[$entry.Trim()] = $true
        }
    }
    
    Write-Host "`nChecking entries from '$sourceFile' against '$targetFile'...`n" -ForegroundColor Cyan
    Write-Host "Results:" -ForegroundColor Yellow
    Write-Host "----------------------------------------"
    
    $foundCount = 0
    $notFoundCount = 0
    
    foreach ($entry in $sourceEntries) {
        $trimmedEntry = $entry.Trim()
        if (-not [string]::IsNullOrWhiteSpace($trimmedEntry)) {
            if ($targetHash.ContainsKey($trimmedEntry)) {
                Write-Host "[FOUND] $entry" -ForegroundColor Green
                $foundCount++
            }
            else {
                Write-Host "[NOT FOUND] $entry" -ForegroundColor Red
                $notFoundCount++
            }
        }
    }
    
    Write-Host "`nSummary:" -ForegroundColor Cyan
    Write-Host "----------------------------------------"
    Write-Host "Total entries checked: $($sourceEntries.Count)"
    Write-Host "Found: $foundCount" -ForegroundColor Green
    Write-Host "Not found: $notFoundCount" -ForegroundColor Red
}

function Select-TextFile {
    param (
        [string]$prompt
    )

    # Get all text files in the current directory
    $textFiles = Get-ChildItem -Filter "*.txt" | Select-Object -ExpandProperty Name
    
    if ($textFiles.Count -eq 0) {
        Write-Host "No text files found in the current directory." -ForegroundColor Red
        return $null
    }
    
    Write-Host "`n$prompt" -ForegroundColor Yellow
    Write-Host "----------------------------------------"
    
    for ($i = 0; $i -lt $textFiles.Count; $i++) {
        Write-Host "$($i + 1). $($textFiles[$i])"
    }
    
    do {
        $selection = Read-Host "`nEnter the number of your selection (1-$($textFiles.Count))"
        
        if ($selection -match '^\d+$' -and [int]$selection -ge 1 -and [int]$selection -le $textFiles.Count) {
            return $textFiles[[int]$selection - 1]
        }
        
        Write-Host "Invalid selection. Please try again." -ForegroundColor Red
    } while ($true)
}

# Main script
Clear-Host

do {
    Write-Host "Text File Comparison Tool" -ForegroundColor Cyan
    Write-Host "========================="
    
    # Select first file
    $file1 = Select-TextFile -prompt "Select the first file:"
    if ($null -eq $file1) { exit 1 }
    Show-FileContents -filePath $file1
    
    # Select second file
    $file2 = Select-TextFile -prompt "Select the second file:"
    if ($null -eq $file2) { exit 1 }
    Show-FileContents -filePath $file2
    
    Write-Host "`nSelected Files:" -ForegroundColor Yellow
    Write-Host "1. $file1"
    Write-Host "2. $file2"
    
    Write-Host "`nComparison Direction:" -ForegroundColor Yellow
    Write-Host "----------------------------------------"
    Write-Host "1. Check which entries from '$file1' exist in '$file2'"
    Write-Host "2. Check which entries from '$file2' exist in '$file1'"
    Write-Host "3. Select different files"
    Write-Host "4. Exit"
    Write-Host "----------------------------------------"
    
    $choice = Read-Host "`nEnter your choice (1-4)"
    
    switch ($choice) {
        "1" {
            Compare-Files -sourceFile $file1 -targetFile $file2
        }
        "2" {
            Compare-Files -sourceFile $file2 -targetFile $file1
        }
        "3" {
            Clear-Host
            continue
        }
        "4" {
            Write-Host "`nExiting script..." -ForegroundColor Yellow
            exit 0
        }
        default {
            Write-Host "`nInvalid choice. Please select 1-4." -ForegroundColor Red
        }
    }
    
    Write-Host "`nPress Enter to continue..."
    Read-Host
    Clear-Host
    
} while ($true)

Previous Post Next Post

نموذج الاتصال