![]() You can now see the document that has changed.Īs you've seen, there are numerous ways to compare the contents of two folders. (Compare-Object -ReferenceObject $SourceDocs -DifferenceObject $DestDocs -Property hash -PassThru).Path By using the entire object rather than just the hash string and specifying the Hash property with the –Property parameter on Compare-Object, I can get the files that represent each hash. Figure 8.īut I need to see the file names! No problem. I'll run the compare again and you can see that the hashes are different now. I'm just going to add a single space to one of them as you can see below. Let's change something in one of the documents. If all files are exactly the same, you will get no output from this command: Compare-Object -ReferenceObject $SourceDocs.Hash -DifferenceObject $DestDocs.Hash Now that we have the hash for each file, it's just a matter of comparing them with Compare-Object. You can now see we've captured the hash of each file in each folder. $SourceDocs = Get-ChildItem –Path C:\Documents1 | foreach I'll now calculate the hash of each file in each folder. I've gone ahead and put that document back in C:\Documents2 to make them in sync again (or so I think). This is the value I need to generate on each file in each folder to make the comparison with. You can see the hash represented by the Hash property. I've picked one of the documents here and calculated it's hash using Get-FileHash. One I'm able to calculate the hash for each file in each folder I can then compare those strings to get an accurate representation of the differences (if any). By using this cmdlet, I can calculate a unique string of numbers and letters uniquely representing each file. PowerShell v4 introduced a handy cmdlet called Get-FileHash. This makes 100% sure the documents are exactly the same in both folders. ![]() The only true way to compare these folders is to check the file hash on each one and compare with each other. You can see there are various ways to do this but each has its downfalls. Even though this might be tempting to leave at this, what if the documents remained the same but the contents were modified? This indicates that the document is in the source but not in the destination. In the above example, I've removed one file to show you how Compare-Object works. This is less likely but still might not be the exact same files. I could also find the total size of each folder because what are the chances of having each folder having files adding up to the exact same size? Figure 3. The file names could be different and could be various sizes. One way I can do this is just ensuring the files in each folder are all Word documents and have the same number of them in each folder. I'll now copy the documents to get them in sync. Once I do this, you can see that $SourceDocs contains all of the files I'd like copied to C:\Documents2 but C:\Documents2 has no files in it at all. $DestDocs = Get-ChildItem –Path C:\Documents2 $SourceDocs = Get-ChildItem –Path C:\Documents1 ![]() An easy way to do this is to only use the Get-ChildItem cmdlet on each folder. To make a proper comparison, regardless of the method, we need to figure out the file names in each of the folders. In this article, I'll start with the easiest yet least precise method to comparing those CEO documents I described earlier. Each depends on just how certain you want to be. ![]() This can be done a couple different ways. You have a requirement where you need to ensure that two folders containing a set of files are in sync. Regardless of the fact this scenario will most likely never happen to you (although I've heard of crazier situations), the requirement does still pop up from time to time. Among other things, you're going to do everything you can to ensure the documents on the server are exactly the same as what's on his desktop. Will you leave anything to chance? Probably not. ![]() And you are the one in charge of copying these documents from the server to his desktop. These documents are what makes or breaks your entire company. He checks these documents every single day. Let's say you've got a PowerShell script that copies and relevant set of documents from a server to your CEO's desktop. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |