Compare two files and report the differences

Sometimes you have two sets of data and you want to compare them, or find the difference. Perhaps you want to get find out which rows are in CSV (or Excel worksheet) but not in another, or you need to compare two columns. Either way, if you export the data to a CSV, powershell can do the rest pretty easily.

The following command uses “diff” which is an alias for “compare-object” to look at two separate files, and export the differences to HTML.

PS C:\> diff (get-content z:\shared\processes.csv) (get-content z:\shared\processes1.csv) | convertto-html > z:\shared\d

If for example you had lists of files, processes, customer records, you could use this pretty easily to see the difference.

Of course this actually works better too if you work with XML, because you can then use the “-property” parameter to focus on individual parts of each line (the column in Excel-speak).

So something like this:

PS C:\process> diff (import-clixml c:\process\process1.xml) (import-clixml c:\process\process2.xml) -property Name | con
vertto-html > c:\process\process.html

Leave a comment

Filed under Powershell

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s