Monthly Archive

Categories

Comparing folder contents

I’m in the process of re-organising (again) my script collection and wanted to check what I had in various folders.  I needed to check what I had in two versions of the same folder. Back in the day I had a really good DOS based utility that would do this for me – but I can’t find it anymore. So need to compare – we have a compare-object cmdlet.  Quick and easy function

001
002
003
004
005
006
007
008
009
010
011
012
013
014
function Compare-Folder {
param (
[string]$folder1,
[string]$folder2
)

$folder1, $folder2 | foreach {if (-not(Test-Path $_)){Throw "Folder: $_ not found"}}

$content1 = Get-ChildItem $folder1
$content2 = Get-ChildItem $folder2 

Compare-Object -ReferenceObject $content1 -DifferenceObject $content2 -IncludeEqual

}

 

Take the folders as parameters – check they exist.  Get the contents of each folder. I’m deliberately not doing a –recurse as that gets too complicated for what I need and then use compare-object on the listings.  I used –include equal so I could see which files were common.  Its not perfect and it only checks names but its simple and does what I need for now

Leave a Reply