Table of Contents
Comparing Contents of Two Folders in PowerSHell
PowerShell is a powerful tool that can help system administrators and IT professionals automate routine tasks, and one of the common tasks is comparing the contents of two folders. This can be useful for identifying differences between two directory versions or finding missing files in a backup.
To proceed further, it is crucial to know the items in the current directory. For example, the current directory for this demonstration is located at C:\Test1 and C:\Test2.
- C:\Test1 consists of two text files and folders named
FolderAandFolderB. The items in the directory includeFile1.txtandFile2.txt.- C:\Test1\FolderA – It contains one text file (
File4.txt). - C:\Test1\FolderB – It has one text file (
File5.txtandFile7.txt).
- C:\Test1\FolderA – It contains one text file (
- C:\Test2 consists of two text files and folders named
FolderCandFolderD. The items in the directory includeFile1.txtandFile3.txt.- C:\Test2\FolderC – It contains one text file (
File5.txt). - C:\Test2\FolderD – It also contains one text file (
File6.txt).
- C:\Test2\FolderC – It contains one text file (
So now you know the directories details, let’s continue learning to compare the content of two folders.
Use Compare-Object Cmdlet
Use compare-object to compare two folders in PowerShell. compare-object cmdlet compares two data sets in PowerShell.
|
1 2 3 4 5 6 7 8 9 |
$folder1 = "C:\Test1" $folder2 = "C:\Test2" $folder1Items = Get-ChildItem $folder1 -Recurse $folder2Items = Get-ChildItem $folder2 -Recurse Compare-Object -ReferenceObject $folder1Items -DifferenceObject $folder2Items -Property Name -IncludeEqual |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Name SideIndicator ---- ------------- File1.txt == File5.txt == FolderC => FolderD => File3.txt => File6.txt => FolderA <= FolderB <= File2.txt <= File4.txt <= File7.txt <= |
In the above code, we define two folders we want to compare, $folder1 and $folder2. First, the $ symbol creates a variable in PowerShell. Then, the Compare-Object cmdlet compares the two data sets.
The ReferenceObject (Get-ChildItem $folder1) specifies the reference object and the data set that will be used as the basis for comparison. In this case, we are using the Get-ChildItem cmdlet to get a list of files and directories in the $folder1 with -Recurse as parameter and passing that list to the -ReferenceObject parameter of the Compare-Object cmdlet. This tells PowerShell to use the files and directories in $folder1 as the reference for the comparison.
The -DifferenceObject parameter of the Compare-Object cmdlet tells PowerShell to compare the files and directories in $folder2 to those in $folder1. Finally, the -Property specifies the property of the objects that we want to compare. In this case, we compare the Name property of the files and directories in each folder.
The -IncludeEqual specifies that we want to include objects that are the same in both folders in the comparison. By default, the Compare-Object cmdlet only returns objects that differ between the reference and difference objects.
This output indicates that there are two files, three different folders, and one file is the same between two folders. The SideIndicator column in the output shows which side the file or folder is unique to, <= for left side, => for the right side, and == for both.
Considering the above solution, the Compare-Object cmdlet in PowerShell provides an efficient and powerful way to compare and identify differences between two data sets. By specifying the reference and difference objects, the property we want to compare, and any additional options, such as including objects that are the same in both data sets, we can quickly and easily identify changes between two folders.
Further reading:
Use MD5 Hashing
To compare the contents of two folders:
- Use
Get-ChildItemto retrieve all items in the specified directory. - Use
Get-FileHashto generate the MD5 hash value for each file. - Use two
foreachloops to iterate over the hash values for both directories. - In each iteration of every loop:
- Use Where-Object to filter the matching values and print a message if the
ifstatement is fulfilled.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$folder1 = "C:\Test1" $folder2 = "C:\Test2" $hash1 = Get-ChildItem $folder1 -Recurse | Get-FileHash -Algorithm MD5 $hash2 = Get-ChildItem $folder2 -Recurse | Get-FileHash -Algorithm MD5 foreach ($hash in $hash1) { $match = $hash2 | Where-Object { $_.Hash -eq $hash.Hash } if ($match) { Write-Host "File $($hash.Path) exists in both folders and is identical." } else { Write-Host "File $($hash.Path) exists only in $folder1." } } foreach ($hash in $hash2) { $match = $hash1 | Where-Object { $_.Hash -eq $hash.Hash } if (!$match) { Write-Host "File $($hash.Path) exists only in $folder2." } } |
|
1 2 3 4 5 6 7 8 9 |
File C:\Test1\File1.txt exists in both folders and is identical. File C:\Test1\File2.txt exists only in C:\Test1. File C:\Test1\FolderA\File4.txt exists in both folders and is identical. File C:\Test1\FolderB\File5.txt exists in both folders and is identical. File C:\Test1\FolderB\File7.txt exists only in C:\Test1. File C:\Test2\File3.txt exists only in C:\Test2. File C:\Test2\FolderD\File6.txt exists only in C:\Test2. |
MD5 (Message Digest 5) is a widely used cryptographic hash function that generates a fixed-length, 128-bit hash value from a message of any length. Here is a step-by-step explanation of the code, as mentioned earlier:
- The first step is to set the paths to the two folders we want to compare. We create two variables,
$folder1and$folder2, and assign them the paths to the two folders. - Next, we use the
Get-ChildItemcmdlet to retrieve all files in both directories and pipe the output to the Get-FileHash cmdlet, which generates the MD5 hash value for each file. The resulting hash values are stored in the$hash1and$hash2variables. - We then iterate through each hash value in
$hash1and compare it to$hash2. We use the "Where-Object" cmdlet to search for a matching hash value in$hash2, and if a match is found, we output a message to the console indicating that the file exists in both folders and is identical. If a match is not found, we output a message indicating that the file exists only in$folder1. - We then iterate through each hash value in
$hash2and check for files that exist only in$folder2. Next, we use theWhere-Objectcmdlet to search for a matching hash value in$hash1, and if no match is found, we output a message to the console indicating that the file exists only in$folder2.
Considering the above solutions, comparing the contents of two folders in PowerShell can be a powerful tool for identifying differences in file content and structure. Adding MD5 hashing can provide a more accurate comparison of files. With the right script, this task can be performed quickly and easily, even on large and complex directory structures.
That’s all about how to compare contents of two folders in PowerShell