Summary: Windows PowerShell MVP, Dr. Tobias Weltner, provides expert commentary for 2012 Scripting Games Beginner Event 4.

Microsoft Scripting Guy, Ed Wilson, is here. Dr. Tobias Weltner is the expert commentator for Beginner Event 4.

Photo of Tobias Weltner

Dr. Tobias Weltner is Germany’s first Windows PowerShell MVP. Tobias trains and coaches enterprises throughout Europe and helps IT Pros and administrators understand, leverage, and love the Power of PowerShell. He also shares his knowledge on PowerShell.com, and he regularly airs webcasts about exciting Windows PowerShell topics such as multithreading and performance.

As a software developer, he created scripting IDEs to make scripting easier, such as Systemscripter (VBScript), the first graphical IDE for PowerShell (PowerShellIDE), and the award-winning PowerShellPlus, which is now part of Idera, Inc. He provides consultant services to enterprises (Windows PowerShell-enabled applications and solutions). As a Rotarian, Tobias is the youth exchange officer, and he enjoys organizing the world-wide youth exchange. He also likes diving and climbing and is curious to discover new things.

Email: tobias.weltner@email.de
PowerShell.com

The objective is to compare the contents of two file system folders. So the first step is to use Get-ChildItem to read the contents of both folders:

PS> $original = Get-Childitem -Path c:\1
PS> $copy = Get-Childitem -Path c:\2

Next, you'd like to find out which files are different in both folders. Use Compare-Object to only list the objects that are different in both sets, and include the parameter -PassThru to receive the original file objects:

PS> Compare-Object -ReferenceObject $original  -DifferenceObject $copy  -Property Name -PassThru

Compare-Object takes the property (column) that you specify in -Property to distinguish different objects. So we used the -Property Name (the file name) to find files that are not present in the other set. By default, Compare-Object returns only objects that are different and excludes all equal objects.

The Windows PowerShell formatter groups files by directory, so the result is a pretty list of files that are present in only the first or the second folder.

To automatically check whether a backup operation was successful, you could evaluate the result provided by Compare-Object. If Compare-Object returns nothing, you know that there are no differences (both folders contain the same files), and thus the backup operation was successful.

Note the following two special tactics in the final script:

First, it uses @(...) to make sure Compare-Object returns an array. This way, even when Compare-Object does not return anything, the result will be an empty array, which then can be checked by using its Count property.

Second, both sets of files are sorted by the very same property that later is used to compare the sets. This adds more robustness. Because Compare-Object has a so-called "sync window" and can get confused when a large number of differences are encountered, sorting by the comparison property ensures that the number of consecutive difference objects is as small as possible.

So, here's the backup validation script:

$original = Get-Childitem -Path c:\1 | Sort-Object -Property Name

$copy = Get-Childitem -Path c:\2 | Sort-Object -Property Name

 

$difference = @(Compare-Object -ReferenceObject $original -DifferenceObject $copy  -Property Name -PassThru)

 

if ($difference.Count -eq 0) {

    Write-Host -ForegroundColor Green 'Content is equal'

} else {

    Write-Host -ForegroundColor Red 'Content is different. Differences:'

    $difference

}

This returns a green message, which indicates that both folders contain the same files, or a red message, which indicates backup errors and lists the orphan files it found.

~Tobias

The 2012 Scripting Games Guest Commentator Week will continue tomorrow when we will present the scenario for Event 5.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy