2012 Scripting Games Beginner Event 4: Compare Two Folders

Or “My first 5 star script”

The scenario in this event was to compare 2 the contents of 2 folders to make sure that the files where being copied across successfully. The full scenario is listed here

The design points are listed below and quite easy to fulfil.

  • You should return file information objects as the result of your comparison.
  • You only need to compare file names, not file sizes, dates, or contents.
  • Your output should list files that exist in one folder, but not in the other folder.
  • Because the object is to prove to your boss that your backup works, you do not need to write a complicated script. Unnecessary complexity will cost you points.
  • Extra points for using native Windows PowerShell cmdlets, and for simplicity of code.
    The cmdlet we need for this is

Now we need to add in the reference and difference objects

Compare-Object -ReferenceObject C:\2 -DifferenceObject C:\1

This will only show that folders 1 and 2 are different, so what we need to do is get the contents of the folder

Compare-Object -ReferenceObject (Get-ChildItem C:\2)
-DifferenceObject (Get-ChildItem C:\1)

(Get-ChildItem C:\2) will process the contents of the folder before comparing them. This gets around having to create a variable and making the script unnecessarily complex.

Finally we add in the –PassThru to pass through the output to the console

Compare-Object -ReferenceObject (Get-ChildItem C:\2)
-DifferenceObject (Get-ChildItem C:\1) –PassThru


And there is my 5 star script for Beginner Event 4 of the 2012 Scripting Games


About Phill McSherry
Phill McSherry has been working in the Australian IT industry for over 20 years and is the technical manager and solutions architect for managed services provider Titan Solutions - www.titansolutions.com.au

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: