2012 Scripting Games Beginner Event 4: Compare Two Folders

 2012 Scripting Games badge

Summary: In Beginner Event 4, you are required to compare two folders after completing to a backup.

About this event



Date of Event

4/5/2012 12:01 AM

Due Date

4/12/2012 12:01 AM


Event scenario

You are a network administrator for a small company. Each night you use a script to perform a backup of critical data files. This backup copies the contents of the data directory to a networked location. This is a file-based backup, and each night a complete backup occurs. You are not doing a differential or an incremental backup. Recently, your boss has been expressing doubts about this procedure.

Your task is to use Windows PowerShell to illustrate to your boss that the content of the network backup folder is the same as the content of the data folder. Because file sizes and write times change all the time, you only need to prove to your boss that files with the same names exist in both locations. To meet your bosses’ requirements, your solution merely needs to point out files in either folder that do not exist in the other folder. The output that is shown here is acceptable.

Image of command output

The following script should be used to create two folders for you to compare.


# This script creates two folders off of the root.

# It then creates 20 empty files in each folder.

# Next it deletes one random file from each folder.


$path = “C:\”

1..2 |

% {

 Set-Location -Path (md -path “$path\$_”).fullname

 1..20 | % {New-Item -Name “$_.txt” -ItemType file}

 Remove-Item -Path (“{0}.txt” -f (Get-Random -Minimum 1 -Maximum 20))



Design points

  • 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.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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. Good luck as you compete in this year’s Scripting Games. We wish you well.

Ed Wilson, Microsoft Scripting Guy 

Comments (34)

  1. Anonymous says:

    Is it safe to use the cmlet with c:1 and c:2. I mean can we use these two folders to do the comparison? As hardcoded or we need to do something more?

  2. mredwilson says:

    @Brad Hmmmmmm ……. keep at it. You will get it. I have confidence …

  3. mredwilson says:

    @Brad Yes, a one-liner can gain full credit. I do not subtract for aliases, nor do i add for aliases.

  4. Anonymous says:


    I'm having the same problem. I've "submitted" 5 or 6 times with no success…

  5. mredwilson says:

    @Erunama, @JlSuperman, @Joshua_Taylor yes it seems several people are having problems uploading. i will post back when it is back working again.

  6. mredwilson says:

    W.Sterling You still have plenty of time to enter all the events. The events are published Monday  through Friday for 2 weeks. You have 7 calandar days to complete each event. For example Monday April 2nd Event is open until 12:01 April 9th. Be sure to check your time zone though.

  7. Anonymous says:

    @Brad, I was the same but managed to suss it eventually, make sure to read the Get-Help -online for the cmdlet you're running and have a good read through the parameters.

  8. Anonymous says:

    Then I should log in here too. Thank you IamMred. I am very new to PowerShell. Started on the last day of registration and I am hoping to get at least 3 points before the games are over.

  9. Anonymous says:

    I think there is conflicting information being given:

    Think the boss is LOOKING over your sholder while you are typing, and he is tapping his fingers all the while.


    make sure to read the Get-Help -online for the cmdlet you're running and have a good read through the parameters

    Who is going to read through the online help with their boss looking over their shoulder? =)

  10. Anonymous says:

    hey Scripting Guy/Gal (or anyone else who can explain this)… I had a question which I hope doesn’t hint too much. I notice in the help example #2 for compare-object the following:
    PS C:>compare-object -referenceobject $(get-content C:testtestfile1.txt) -differenceobject $(get-content

    Before the (get-content) cmdlets I see a $ character yet I was able to run the same command with the same results without having the $ character. Could you explain what the $ character does in this case?

    Thank you!

  11. mredwilson says:

    @Jeff Wouters no a script is not a requirement. If you can write a one linner and meet all the requirements then one line should work.

  12. Anonymous says:

    Anyone else having trouble submitting their scripts for this event?  I click on the Save button, and get sent back to the landing page, but I don't see the new script listed (only my submissions for previous days).

  13. Anonymous says:

    Yep, having the same problem. Can't post my script for this event.

  14. mredwilson says:

    PoshCode is back up and running. Thank you everyone for your understanding. My apologies for any inconvenience

  15. mredwilson says:

    @Dawn Villejoin I did :-). Yes, you are able to assume that the judges run the file to set up the scenario. I did not want to burden you with the requirements of checking for folders existence … this is why I wrote the setup script in the first place, so you could focus on the task of comparing the folders.

  16. Anonymous says:

    Are aliases in one-liners permitted?

  17. Anonymous says:

    Ahh…. that's better.

  18. mredwilson says:

    @Brian Sabatini The output shown is acceptible. Keep in mind that this event has a design point of Keep it simple, and that Unnessary complexity will COST you. Therefore, if you can create an acceptible output that displays the three pieces of information WITHOUT adding any complexity you are welcome to try. I do not want to mislead you, however, because if you invest too much code here you will lose points due to complexity. Note this is actually mentioned TWICE in the design points: simplicity, AND avoiding unnessary complexity. Keep in mind the goal, you ONLY need to quickly prove to the boss that the backup is working! That is the goal — that is your task. YOur goal, your task, is NOT to produce a beautiful chart for the quarterly stock holders meeting! It is all about balance. Here you want to get the job done as quickly as possible. Think the boss is LOOKING over your sholder while you are typing, and he is tapping his fingers all the while.

  19. mredwilson says:

    @Jlsuperman yes, if the design guidelines state that a one line script will suffice, then because it is a one line aliases are permitted. Positional parameters would also be permitted. I do not count off if you use or if you do not use aliases. I know they are confusing for some people, especially for beginners. One of the great things about PowerShell is that it is very readable, and aliases generally take away from that readability. Keep in mind, however, that you must NOT use a custom alias that would not exist on the judges system. Therefore be especially careful when using aliases IF you define your own aliases in your profile. It is easy to forget that your special aliases is one that your created yourself when it is always available.

  20. mredwilson says:

    @Eletheria yes you can hardcode for this event.

  21. Anonymous says:

    Not so sure if the 'delete a random file' is a good idea.  I've just spent ages trying to figure out why my script wasn't displaying any differences until I realised it had deleted 2.txt from both folders! Argh!

    Will add 2.txt back on one and delete another instead.  🙂

  22. mredwilson says:

    @W. Sterling no there are still problems submitting scripts (and Joel has been working on it for hours now), at least if you are using Live ID as your authentication mechanism. I have seen some new scripts from people who were using google or facebook. BUT DO NOT change your login ID, or else you will loose your score

  23. Anonymous says:

    Thanks Jeff!

  24. Anonymous says:

    Me too.  Will try again later as deadline's a ways off yet.

  25. Jeff Wouters says:

    Hi Ed, Is a script a requirement or is a one liner allowed (without alliasses)? So either XX or 1 line of code 😉

    Since the goal is just to prove that the backup works one line is less work…

  26. Scott Baker says:

    Ha!  Mine randomly deleted the same file in each folder!  Had me confused for a second that my code was off, until I actually looked.  Make sure you double-check and don't fall for Ed Wilson's tricks!

  27. Jeff Wouters says:

    @jlsuperman: Aliasses in one liners will add unnecessary complexity and therefor may cost you points. Feedback I'd got at event 1 from one of the judges that aliasses in scripts are 'not done' but in one liners are allowed.

  28. brad says:

    Wow this one has me… so easy to compair the 2 folders… so hard to get an output that resembles anyth like the example image

  29. Dawn Villejoin says:

    @Brad I thought the same thing. It had me stumped for a good while! Then I actually just tried a few things after reading help files and surprised myself with the results! LOL hang in there. It'll come to you!

  30. Dawn Villejoin says:

    This may be a silly question, but are we to assume that the judges have run the SetupBeginnerEvent4.ps1 file on their end?  

  31. W. Sterling says:

    I know that there were some issues submitting scripts earlier. Has it been resolved? I can't see my submission. Should I submit it again?

  32. Dawn Villejoin says:

    @IamMred Thanks!!  I just wanted to verify 🙂  All I could imagine were errors "File already exists" cropping up on the judges' screens… didn't want that!

  33. Brian Sabatini says:

    The event description states that "the output that is shown here is acceptable" but is it the only output that is allowed? I can come up with other outputs I like more, but don't want to lose points over it.

  34. brad says:

    "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."

    Does this mean that if we submit a one-liner with with no comment based help we will get full point provided that we meet the objectives or does it still need to be in a script format?

    Also is there any disadvantage to using the full command and parameter names though some parameters do not need to be defined.  For example   command alias info1 info2 …. will work but I submit Full-Command -param1 info1 -param2 info2 …

    I prefer all the info so it is very readable but can chop it if it will cost point