Skip to main content
TopMiniSite

Back to all posts

How to Compare Two Folders And Add/Remove Files With Powershell?

Published on
7 min read
How to Compare Two Folders And Add/Remove Files With Powershell? image

Best Powershell Tools to Compare Folders to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

BUY & SAVE
$47.49 $49.99
Save 5%
PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms
4 Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

BUY & SAVE
$27.00 $59.99
Save 55%
Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts
5 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$20.52 $44.99
Save 54%
Learn PowerShell Toolmaking in a Month of Lunches
6 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
7 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$34.99
Learn Windows PowerShell in a Month of Lunches
8 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
9 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$31.99 $44.99
Save 29%
Learn Windows PowerShell in a Month of Lunches
+
ONE MORE?

To compare two folders and add/remove files with PowerShell, you can use the Compare-Object cmdlet. This cmdlet compares two sets of objects and indicates differences. You can use it to compare files in two folders by specifying the folders as the input objects.

First, use the Get-ChildItem cmdlet to retrieve the files in each folder and store them in variables. For example:

$folder1 = Get-ChildItem -Path C:\Folder1 $folder2 = Get-ChildItem -Path C:\Folder2

Next, use the Compare-Object cmdlet to compare the two sets of files. You can specify whether you want to compare by content, property, or both. For example:

$comparison = Compare-Object -ReferenceObject $folder1 -DifferenceObject $folder2 -Property Name -PassThru

This will give you a list of objects that represent the differences between the two folders. You can then loop through these objects and perform actions based on whether the files should be added or removed. For example, to copy files from the first folder to the second folder:

foreach ($file in $comparison) { Copy-Item -Path $file.FullName -Destination C:\Folder2 }

Similarly, you can use the Remove-Item cmdlet to delete files from one folder if necessary. You can also customize the comparison logic based on your specific requirements.

How to compare folders recursively in PowerShell?

To compare folders recursively in PowerShell, you can use the Compare-Object cmdlet along with the -Recurse parameter to compare the contents of two folders recursively.

Here is an example code snippet that demonstrates how to compare folders recursively in PowerShell:

$folder1 = Get-ChildItem -Path "C:\Folder1" -Recurse $folder2 = Get-ChildItem -Path "C:\Folder2" -Recurse

Compare-Object $folder1 $folder2 -Property Name, FullName, Length, LastWriteTime

In this example, the Get-ChildItem cmdlet is used to retrieve the contents of two folders (Folder1 and Folder2) recursively. The Compare-Object cmdlet is then used to compare the contents of the two folders based on properties such as Name, FullName, Length, and LastWriteTime.

You can customize the properties to be compared based on your specific requirements. Additionally, you can use the -ExcludeDifferent or -IncludeEqual parameters with the Compare-Object cmdlet to further refine the comparison results.

How can I compare two folders and add/remove files using PowerShell?

You can use the Compare-Object cmdlet in PowerShell to compare two folders and then add or remove files based on the comparison results. Here's an example of how you can do this:

  1. First, you need to define the two folders that you want to compare. You can use the Get-ChildItem cmdlet to get a list of files in each folder:

$folder1 = Get-ChildItem -Path "C:\Folder1\" $folder2 = Get-ChildItem -Path "C:\Folder2\"

  1. Next, use the Compare-Object cmdlet to compare the two folders. You can specify the -Property parameter to compare the files based on their names or other properties:

$comparison = Compare-Object -ReferenceObject $folder1 -DifferenceObject $folder2 -Property Name -PassThru

  1. Finally, you can iterate through the comparison results to add or remove files as needed. For example, to add files from Folder1 to Folder2, you can use the following command:

foreach ($file in $comparison) { Copy-Item -Path $file.FullName -Destination "C:\Folder2\"
}

Similarly, to remove files from Folder2 that are not present in Folder1, you can use the following command:

foreach ($file in $comparison) { Remove-Item -Path $file.FullName
}

This is just a basic example of how you can compare two folders and add or remove files using PowerShell. Depending on your specific requirements, you may need to customize the script further.

How to use PowerShell to compare two folders?

To compare two folders using PowerShell, you can use the Compare-Object cmdlet. Here's how you can do it step by step:

  1. Open PowerShell ISE or PowerShell as an administrator.
  2. Use the following command to compare two folders:

Compare-Object -ReferenceObject (Get-ChildItem -Path "path\to\first\folder") -DifferenceObject (Get-ChildItem -Path "path\to\second\folder")

Make sure to replace "path\to\first\folder" and "path\to\second\folder" with the actual paths of the folders you want to compare.

  1. The output of the above command will show you the differences between the two folders. The differences will be displayed in a side-by-side view with the "SideIndicator" column showing which folder the item belongs to (<= for the reference folder, => for the difference folder).
  2. You can also use the "-IncludeEqual" parameter to include the items that are the same in both folders in the output:

Compare-Object -ReferenceObject (Get-ChildItem -Path "path\to\first\folder") -DifferenceObject (Get-ChildItem -Path "path\to\second\folder") -IncludeEqual

  1. You can also customize the output further by using the various parameters of the Compare-Object cmdlet. For example, you can use the "-Property" parameter to compare specific properties of the items in the folders.

That's it! You have now successfully compared two folders using PowerShell.

How to compare two folders and delete redundant files in PowerShell?

To compare two folders and delete redundant files in PowerShell, you can follow these steps:

  1. Use the Compare-Object cmdlet to compare the contents of the two folders. This cmdlet will show which files are present in one folder but not the other.
  2. Use a loop to iterate through the results of the Compare-Object cmdlet and identify the redundant files.
  3. Use the Remove-Item cmdlet to delete the redundant files from the folder.

Here is an example PowerShell script that compares two folders and deletes redundant files:

$folder1 = "C:\Folder1" $folder2 = "C:\Folder2"

$redundantFiles = Compare-Object (Get-ChildItem $folder1) (Get-ChildItem $folder2) -Property Name, FullName | Where-Object { $_.SideIndicator -eq "=>" }

foreach ($file in $redundantFiles) { $filePath = $file.FullName Remove-Item $filePath -Force Write-Output "Deleted redundant file: $filePath" }

In this script, replace "C:\Folder1" and "C:\Folder2" with the paths of the two folders you want to compare. The script will then compare the contents of the two folders and delete any redundant files found in the first folder.

Please note that this script will permanently delete the redundant files, so make sure to back up any important files before running it.

How to compare two folders and identify duplicates using PowerShell?

Here is a script that compares two folders and identifies duplicate files using PowerShell:

  1. Open PowerShell ISE or any text editor to write and save the PowerShell script.
  2. Copy and paste the following script:

$folder1 = "C:\Path\To\Folder1" $folder2 = "C:\Path\To\Folder2"

$files1 = Get-ChildItem -Path $folder1 -File -Recurse $files2 = Get-ChildItem -Path $folder2 -File -Recurse

$hashTable = @{}

foreach ($file in $files1) { $hash = Get-FileHash -Path $file.FullName -Algorithm MD5 $hashTable[$hash.Hash] = $file.FullName }

foreach ($file in $files2) { $hash = Get-FileHash -Path $file.FullName -Algorithm MD5 if($hashTable.ContainsKey($hash.Hash)) { Write-Output "Duplicate file found: $($file.FullName)" Write-Output "Original file path: $($hashTable[$hash.Hash])`n" } }

  1. Modify the $folder1 and $folder2 variables with the paths to the folders you want to compare.
  2. Save the script with a .ps1 extension (e.g., CompareFolders.ps1).
  3. Open PowerShell and navigate to the directory where the script is saved.
  4. Run the script by typing its name (e.g., .\CompareFolders.ps1) and pressing Enter.

The script will compare the files in the two specified folders and output a message for each duplicate file found, along with the path to the original file.

How to check if two folders are identical in PowerShell?

One way to check if two folders are identical in PowerShell is to use the Compare-Object cmdlet to compare the contents of the two folders. Here's how you can do it:

  1. Open PowerShell
  2. Run the following command to compare the contents of the two folders:

Compare-Object -ReferenceObject (Get-ChildItem -Path "C:\folder1" -Recurse) -DifferenceObject (Get-ChildItem -Path "C:\folder2" -Recurse) -Property Name, Length

Replace "C:\folder1" and "C:\folder2" with the paths to the two folders you want to compare.

  1. Look at the output of the command. If the folders are identical, the command will not return any output. If there are differences between the two folders, the command will display the differences.

This method compares the files in the two folders based on their names and lengths. You can modify the Compare-Object cmdlet to compare the files based on different properties if needed.