How to compare two Excel / CSV / XLSX files using PowerShell

The following PowerShell script allows you to compare ALL the columns in two CSV files contents against each other.

cls

# Import the files
 $file1 = import-csv -Path "file1.csv";
 $file2 = import-csv -Path "file2.csv";

# Get the list of properties
 $props1 = $file1 | gm -MemberType NoteProperty | select -expand Name | sort | % {"$_"}
 $props2 = $file2 | gm -MemberType NoteProperty | select -expand Name | sort | % {"$_"}

if(Compare-Object $props1 $props2) {

    # Check that properties match

    throw "Properties are not the same! [$props1] [$props2]"

} else {

    # Pass properties list to Compare-Object

    "Checking $props1"

    Compare-Object $file1 $file2 -Property $props1

}