Home / Blog / Plagiarism & similarity

File and document comparison

Powershell Compare Two Text Files And Output Differences

Compare text lines with PowerShell with a reproducible example, explicit handling of whitespace and revision direction, and checks before saving the reviewed result.

Powershell Compare Two Text Files And Output Differences

Read each text file into a line array and compare those arrays with Compare-Object. The result identifies values found on only one side. This is useful for list membership, but it is not a full editor-style patch showing every insertion at an exact source line number.

Export objects before formatting them

The report below uses Export-Csv so the line value and side indicator remain separate fields. Open the exported file with a CSV parser and verify the two headers. A line containing a comma must remain one InputObject value, not become two report columns. Display formatting belongs at the end of an interactive view; feeding formatted table objects into an export loses the original comparison structure.

Compare text lines with PowerShell

$originalLines = @(Get-Content -LiteralPath ".\original.txt" -Encoding UTF8)
$revisedLines = @(Get-Content -LiteralPath ".\revised.txt" -Encoding UTF8)
$changes = Compare-Object -ReferenceObject $originalLines `
                         -DifferenceObject $revisedLines -CaseSensitive
$changes | Select-Object InputObject, SideIndicator |
    Export-Csv -LiteralPath ".\differences.csv" -NoTypeInformation -Encoding UTF8

Verify a known change

Put apple and banana on separate lines in the original file, then banana and pear in the revised file. The report should associate apple with <=, meaning original/reference only, and pear with =>, meaning revised/difference only. The common banana line is omitted unless you request equal values. Test a capitalized Apple separately because the example explicitly enables case-sensitive comparison.

Choose the right interpretation

The CSV export retains the result properties in a machine-readable form. Do not pipe through Format-Table before Export-Csv, because formatting objects are not the original data rows. For an ordered edit view, use a diff editor or a unified-diff tool. Use LiteralPath for supplied filenames so wildcard characters in a name do not unexpectedly select additional files. The commands read the two inputs and write one report; they do not merge, remove or move source files.

Keep the comparison reproducible

Label the two inputs Original and Revised and keep those roles consistent. Save a copy of each before experimenting with normalization or accepting changes. A reversed comparison can show the same edit with the opposite insertion/deletion direction, which matters when a reviewer is deciding what to retain. Record the two filenames and the settings used with the result.

Test an identical pair first: it should produce no differences under the chosen contract. Then change one ordinary word, one number and one punctuation mark in the revised copy. Verify each known change before using an unfamiliar document. Include a blank line and a moved paragraph so you can see whether the method reports a move or a deletion followed by an insertion. Those presentations may describe the same underlying edit.

Decide whether spaces, capitalization and line endings matter before normalizing them. Ignoring every space can hide a meaningful change in code, a quoted value or a fixed-width record. Case folding is useful for some lookup tasks but is wrong for case-sensitive identifiers. Retain the unmodified inputs alongside any normalized copies so a surprising result can be investigated rather than explained away.

Review the complete changed region, including its surrounding context. An inserted “not,” a removed qualification or a changed unit can reverse a statement while contributing very few changed characters. Conversely, a formatter can change many lines without changing an application’s intended behavior. A difference count describes the chosen representation; it does not by itself measure importance, quality, originality or semantic equivalence.

Finish the review deliberately

For each meaningful difference, decide whether to keep the original, keep the revised version or write a new combined version. Save that reviewed result under a new filename and compare it again with the intended baseline. Do not overwrite the only original while you are still interpreting the report. Keep a short record of unresolved differences when the review is handed to another person.

Use only files you are authorized to inspect. If a web tool is involved, read its stated processing behavior and file limits before supplying confidential material. A local comparison can avoid uploading the content, but browser downloads, clipboard history and shared-device storage still need ordinary care. Clear temporary copies according to your own retention needs after the review is complete.

Primary reference

Official comparison documentation