Powershell: Compare 2 Folders and Remove Matching Files

$Folder1 = Get-ChildItem -Path "C:\Folder1" -filter *.txt
$Folder2 = Get-ChildItem -Path "C:\Folder2" -filter *.txt
$FileList = Compare-Object $Folder1 $Folder2 -IncludeEqual -ExcludeDifferent
foreach($i in $filelist){remove-item -path ("C:\Folder1\" + $i.inputobject)}

$filelist is going to contain a list of file names that were found in both Folder1 and Folder2. In this case I wanted to remove the files in Folder1 that were also found in Folder2.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s