Windows OS

How to Compare Two Directories in Windows

Directories are referred to folders in the Windows system that are the main unit of computer storage. It stores software and Windows program files, user’s textual file, binaries and bak files, and so on. Sometimes we need to compare directories. For example, we have created the backup directory, but some files are missing. As some directories contain so many files, we are unable to compare each file one by one manually. Don’t worry if you find it hard to compare directories manually.

The article will specify the PowerShell commands that are used to compare directories quickly in Windows.

How to Compare Two Directories in Windows?

How to Compare Two Directories in Windows?
To check for updated or missing files, we may need to compare two folders. For instance, to determine whether file has been modified or is missing in another directory, we will compare two folders, “Folder 1” and “Folder 2.” During the comparison process, one directory will serve as the source directory and the other will target.

The content of the directories is shown below one by one:

Folder_1:

Folder_2:

The following steps will compare the above-mentioned directories.

Step 1: Open Windows PowerShell
Open up the Startup menu and type “Windows PowerShell”. Select “Windows PowerShell” and launch it:

Step 2: Collect Directories Content
Save the contents of the directories in new variables. Use the “Get-ChildItem” command, which is designed to retrieve or access content or object information from directories. We have use “-Recurse” option that will get information recursively from subdirectories:

> $dir1 = Get-ChildItem -Recurse -path C:\Users\anuma\Folder_1
> $dir2 = Get-ChildItem -Recurse -path C:\Users\anuma\Folder_2

Step 3: Compare Two Directories
Compare two directories using the “Compare-Object” command. The source directory will be mentioned with “ReferenceObject” and targeted will be with “-DifferenceObject”. In our case, “dir1” refers to the “InputObject” where “dir2” acts as a “DifferenceObject”.

> Compare-Object -ReferenceObject $dir1 -DifferenceObject $dir2

From the below-given output, the “InputObject” is showing the “DifferenceObject”. The outgoing arrows show that these files are missing in “ReferenceObject”. This indicates that “Folder 1” is missing these two files:

Let’s swap the “ReferenceObject” and “DifferenceObject” parameters:

> Compare-Object -ReferenceObject $dir2 -DifferenceObject $dir1

Notice the direction of arrows from below given output. It means “DifferenceObject” requires a copy of these files or that certain files are missing.

Alternatively, you can use the “compare” command as follows. The command uses “compare” to compare two objects that are being passed to it using the “Get-ChildItem” cmdlet:

> compare (Get-ChildItem -r C:\Users\anuma\Folder_1) (Get-ChildItem -r C:\Users\anuma\Folder_2)

We have taught you how to compare two folders in Windows in the simplest possible method.

Conclusion

It is hard to compare two directories manually. To compare directories in an effective manner, you can compare two directories quickly using Windows PowerShell cmdlets. The “Compare-Object” is the primary cmdlet used to compare directories whereas the “Get-ChildItem” is also used with it to access the directories data or objects. Here, we have demonstrated the method to compare the directories in windows.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.