{"id":6931,"date":"2015-04-01T00:01:00","date_gmt":"2015-04-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/04\/01\/use-powershell-to-compare-two-files\/"},"modified":"2019-02-18T10:30:04","modified_gmt":"2019-02-18T17:30:04","slug":"use-powershell-to-compare-two-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-compare-two-files\/","title":{"rendered":"Use PowerShell to Compare Two Files"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to compare two files.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I have a script that I wrote to compare two files, but it seems really slow. I am wondering what I can do to speed things up a bit.<\/p>\n<p>&mdash;JW<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello JW,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I looked at the script you supplied, where you use <b>Compare-Object<\/b> to compare two files. Here is your script:<\/p>\n<p style=\"margin-left:30px\">$fileA = &quot;C:\\fso\\myfile.txt&quot;<\/p>\n<p style=\"margin-left:30px\">$fileB = &quot;C:\\fso\\CopyOfmyfile.txt&quot;<\/p>\n<p style=\"margin-left:30px\">$fileC = &quot;C:\\fso\\changedMyFile.txt&quot;<\/p>\n<p style=\"margin-left:30px\">if(Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileB))<\/p>\n<p style=\"margin-left:30px\">&nbsp;{&quot;files are different&quot;}<\/p>\n<p style=\"margin-left:30px\">Else {&quot;Files are the same&quot;}<\/p>\n<p>When I run the script and compare FileA with FileB, the script returns the correct response:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-1-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-4-1-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>When I change it to use FileC, the script also works:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4885.hsg-4-1-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4885.hsg-4-1-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><span style=\"font-size:12px\">The three files are shown here:<\/span><\/p>\n<p><span style=\"font-size:12px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3005.hsg-4-1-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3005.hsg-4-1-15-03.png\" alt=\"Image of files\" title=\"Image of files\" \/><\/a><br \/><\/span><\/p>\n<p><span style=\"font-size:12px\">So JW, this is a very simple test case. What is really going on when using <\/span><b style=\"font-size:12px\">Compare-Object<\/b><span style=\"font-size:12px\">?<\/span><\/p>\n<p>I can use the Windows PowerShell ISE to run a portion of the code and look at it. To do this, I highlight the <b>Compare-Object<\/b> statement and press F-8 to execute only that portion of the code. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileC)<\/p>\n<p style=\"margin-left:30px\">InputObject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbs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\"margin-left:30px\">Additional values&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;=&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>And when I compare FileA with FileB, the following appears:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Compare-Object -ReferenceObject $(Get-Content $fileA) -DifferenceObject $(Get-Content $fileB)<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;&nbsp;<\/p>\n<p>This triggers the <b>ELSE<\/b> portion of the code. Although this works, it can be a bit slow, and on more complex files, I would think it would also be a bit unreliable.<\/p>\n<p>So a better way to do this is to use <b>Get-FileHash<\/b> and compare the <b>HASH<\/b> property. Your revised script is shown here:<\/p>\n<p style=\"margin-left:30px\">$fileA = &quot;C:\\fso\\myfile.txt&quot;<\/p>\n<p style=\"margin-left:30px\">$fileB = &quot;C:\\fso\\CopyOfmyfile.txt&quot;<\/p>\n<p style=\"margin-left:30px\">$fileC = &quot;C:\\fso\\changedMyFile.txt&quot;<\/p>\n<p style=\"margin-left:30px\">if((Get-FileHash $fileA).hash&nbsp; -ne (Get-FileHash $fileC).hash)<\/p>\n<p style=\"margin-left:30px\">&nbsp;{&quot;files are different&quot;}<\/p>\n<p style=\"margin-left:30px\">Else {&quot;Files are the same&quot;}<\/p>\n<p>Now, when I look at the portion of the code that executes, I can see that I am dealing with a Boolean, instead of trying to evaluate whether output (which is basically ignored) appears or not (as in your previous script).<\/p>\n<p>In the following, I execute only the <b>Get-FileHash<\/b> portion of the script:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (Get-FileHash $fileA).hash&nbsp; -ne (Get-FileHash $fileC).hash<\/p>\n<p style=\"margin-left:30px\">True<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (Get-FileHash $fileA).hash&nbsp; -ne (Get-FileHash $fileB).hash<\/p>\n<p style=\"margin-left:30px\">False<\/p>\n<p>In addition, the <b>Get-FileHash<\/b> code is rather efficient because Windows PowerShell is pretty fast when it comes to getting the file hash. Plus this operation simply obtains the file hashes, and compares the two hashes. Your original script reads in the complete file, and then compares it line-by-line, so it is much less efficient.<\/p>\n<p>JW, that is all there is to using Windows PowerShell to compare two files. Troubleshooting Week will continue tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to compare two files. &nbsp;Hey, Scripting Guy! I have a script that I wrote to compare two files, but it seems really slow. I am wondering what I can do to speed things up a bit. &mdash;JW &nbsp;Hello JW, Microsoft Scripting Guy, Ed Wilson, [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,45],"class_list":["post-6931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to compare two files. &nbsp;Hey, Scripting Guy! I have a script that I wrote to compare two files, but it seems really slow. I am wondering what I can do to speed things up a bit. &mdash;JW &nbsp;Hello JW, Microsoft Scripting Guy, Ed Wilson, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=6931"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/6931\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=6931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=6931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=6931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}