{"id":9261,"date":"2012-05-31T00:01:00","date_gmt":"2012-05-31T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/05\/31\/use-powershell-to-compute-md5-hashes-and-find-changed-files\/"},"modified":"2012-05-31T00:01:00","modified_gmt":"2012-05-31T00:01:00","slug":"use-powershell-to-compute-md5-hashes-and-find-changed-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-compute-md5-hashes-and-find-changed-files\/","title":{"rendered":"Use PowerShell to Compute MD5 Hashes and Find Changed Files"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to compute MD5 hashes and find files changed in a folder.<\/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 folder and I would like to detect if files within it have changed. I do not want to write a script to parse file sizes and dates modified because that seems to be a lot of work. Is there a way I can use an MD 5 hash to do this? Oh, by the way, I do have a reference folder that I can use.<\/p>\n<p>&mdash;RS<\/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 RS,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Things are certainly beginning to get crazy. In addition to all of the normal end-of-the-year things going on around here, in addition to the middle of a product ship cycle, we are entering &ldquo;conference season.&rdquo; This morning, the Scripting Wife and I (along with a hitchhiker from the Charlotte Windows PowerShell User Group) load up the car and head to <a href=\"http:\/\/www.cmschill.net\/StringTheory\/uncategorized\/announcing-atlanta-techstravaganza-2012\/\" target=\"_blank\">Atlanta, Georgia for TechStravaganza<\/a>. We have the speaker&rsquo;s dinner this evening, and tomorrow we will be flat out all day as the event kicks off. It will be a great day with one entire track devoted to Windows PowerShell. The following week, we head to Florida for a <a href=\"http:\/\/www.sqlsaturday.com\/132\/eventhome.aspx\">SQL Saturday<\/a>, <a href=\"http:\/\/northamerica.msteched.com\/#fbid=RDyDMd2ceCG\" target=\"_blank\">Microsoft TechEd<\/a>, and <a href=\"http:\/\/itprocamp.com\/jacksonville\/\" target=\"_blank\">IT Pro Camp<\/a>. In fact, our Florida road trip begins with the monthly meeting of the <a href=\"http:\/\/powershellgroup.org\/charlotte.nc\" target=\"_blank\">Charlotte Windows PowerShell User Group<\/a> (we actually leave for our trip from the group meeting). If you find all this a bit confusing, I do too. That is why I am glad we have the <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/hh182567\" target=\"_blank\">Scripting Community page<\/a>, so I can keep track of everything.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>This is the fourth in a series of four Hey, Scripting Guy! blogs about using Windows PowerShell to facilitate security forensic analysis of a compromised computer system. The intent of the series is not to teach security forensics, but rather to illustrate how Windows PowerShell could be utilized to assist in such an inquiry. The first blog discussed <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/05\/28\/use-powershell-to-aid-in-security-forensics.aspx\" target=\"_blank\">using Windows PowerShell to capture and to analyze process and service information<\/a>. &nbsp;The second blog talked about <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/05\/29\/use-powershell-to-perform-offline-analysis-of-security-logs.aspx\" target=\"_blank\">using Windows PowerShell to save event logs in XML format and perform offline analysis<\/a>. The third blog talked about <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/05\/30\/learn-the-easy-way-to-use-powershell-to-get-file-hashes.aspx\" target=\"_blank\">computing MD5 hashes for files in a folder<\/a>.<\/p>\n<h2>The easy way to spot a change<\/h2>\n<p>It is extremely easy to spot a changed file in a folder by making a simple addition to the technique discussed yesterday. In fact, it does not require writing a script. The trick is to use the <b>Compare-Object<\/b> cmdlet. In the image that follows, two folders reside beside one another. The <b>Ref<\/b> folder contains all original files and folders. The <b>Changed<\/b> folder contains the same content, with a minor addition made to the a.txt file.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4213.hsg-5-31-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4213.hsg-5-31-12-01.png\" alt=\"Image of menus\" title=\"Image of menus\" \/><\/a><\/p>\n<p>After you import the PSCX, use the <b>Compare-Object<\/b> cmdlet to compare the hashes of the c:\\ref folder with the hashes of the c:\\changed folder. The basic command to compute the hashes of the files in each folder was discussed in yesterday&rsquo;s blog. The chief difference here is the addition of the <b>Compare-Object<\/b> cmdlet. The command (a single logical command) is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Compare-Object -ReferenceObject (dir c:\\ref -Recurse | Where-Object {!$_.psis<\/p>\n<p style=\"padding-left: 30px\">container } | get-hash) -differenceObject (dir c:\\changed -Recurse | Where-Object {!$<\/p>\n<p style=\"padding-left: 30px\">_.psiscontainer } | get-hash)<\/p>\n<p>The command and the associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5381.hsg-5-31-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5381.hsg-5-31-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The command works because the <b>Compare-Object<\/b> cmdlet knows how to compare objects, and because the two <b>Get-Hash<\/b> commands return objects. The arrows indicate which object contains the changed objects. The first one exists only in the <b>Difference<\/b> object, and the second one only exists in the <b>Reference<\/b> object.<\/p>\n<h2>Find the changed file<\/h2>\n<p>Using the information from the previous command, I create a simple filter to return more information about the changed file. The easy way to do this is to highlight the hash, and place it in a <b>Where-Object<\/b> command (the <b>? <\/b>is an alias for <b>Where-Object<\/b>). I know from yesterday&rsquo;s blog, that the property containing the MD5 hash is called <b>hashstring<\/b><i>, <\/i>and therefore, that is the property I look for. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; dir c:\\changed -Recurse | Where-Object {!$_.psiscontainer } | get-hash | ? {<\/p>\n<p style=\"padding-left: 30px\">$_.hashstring -match &#8216;DE1278022BF9A1A6CB6AAC0E5BEE1C5B&#8217;}<\/p>\n<p>The command and the output from the command are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1665.hsg-5-31-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1665.hsg-5-31-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Finding the differences in the files<\/h2>\n<p>I use essentially the same commands to find the differences between the two files. First, I make sure that I know the reference file that changed. Here is the command that I use for that:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; dir c:\\ref -Recurse | Where-Object {!$_.psiscontainer } | get-hash | ? { $_.h<\/p>\n<p style=\"padding-left: 30px\">ashstring -match &#8217;32B72AF6C2FF057E7C63C715449BFB6A&#8217;}<\/p>\n<p>When I have ensured that it is, in fact, the a.txt file that has changed between the reference folder and the changed folder, I again use the <b>Compare-Object<\/b> cmdlet to compare the content of the two files. Here is the command I use to compare the two files:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Compare-Object -ReferenceObject (Get-Content C:\\Ref\\a.txt) -DifferenceObjec<\/p>\n<p style=\"padding-left: 30px\">(Get-Content C:\\Changed\\a.txt)<\/p>\n<p>The image that follows illustrates the commands and the output associated with these commands.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2450.hsg-5-31-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2450.hsg-5-31-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>RS, that is all there is to using finding modifications to files in folders when you have a reference folder.&nbsp; Join me tomorrow for more cool stuff in the world of Windows PowerShell.<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to compute MD5 hashes and find files changed in a folder. &nbsp;Hey, Scripting Guy! I have a folder and I would like to detect if files within it have changed. I do not want to write a script to parse file sizes and dates modified because that seems [&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,63,45],"class_list":["post-9261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-security","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to compute MD5 hashes and find files changed in a folder. &nbsp;Hey, Scripting Guy! I have a folder and I would like to detect if files within it have changed. I do not want to write a script to parse file sizes and dates modified because that seems [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9261","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=9261"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9261\/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=9261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}