Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions Links


SQL Server DBA Jobs

Compare two files with Powershell

08 May,2012 by Tom Collins

Question: How can I compare two files and report on differences using Powershell ? I have two large T-SQL scripts , which both parse successfully on SSMS , but give me different results

Answer:   Comparing two files is a common DBA task. Use a  combination of  Powershell Get-Content and Compare-Object.

Get-Content gets the content of the file at the specified location

Compare-Object compares the two items and reports on the differences

 Create two variables and use Get-Content to assign the values. Compare the two variables using Compare-Object . Output the results to a file for analysis.


$strPrimary = Get-Content C:\primary.sql
$strSecondary = Get-Content  C:\secondary.sql
Compare-Object $strPrimary $strSecondary >C:\compareResults.txt

InputObject                                                 SideIndicator                                              
-----------                                                 -------------                                              
 GROUP BY mem.[first]                                         =>                                                         
 GROUP BY mem.[last]                                        <=                                                         

Author: Tom Collins (


Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment on Compare two files with Powershell | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer