Powershell that compairs folders

Posted on 2012-09-14
Last Modified: 2012-10-24
I'm putting together a power shell that compares two folders, but I'm having a bit of trouble getting it do to what I want.

What I"m looking to do is compare folder A with folder B and list all the files that are different.  In the list would be the folder location, file, and date/time stamp.  So it would look like :

Folder A                 Timestamp                        Folder B                  Timestamp
c:\temp\file.txt      9/15/2012/ 7:11 AM         x:\some\file.txt      9/15/2012/ 8:04 AM

Question by:Mach03
    LVL 16

    Expert Comment

    by:Dale Harris
    I believe the meat and potatoes can be found in this article I wrote a while back:

    Good luck!

    LVL 67

    Accepted Solution

    You should think it is an easy task with PowerShell - but it isn't.
    Using associative arrays (hash tables), we can say
    $folderA = 'C:\temp\ee\tst\1'
    $folderB = 'C:\temp\ee\tst\2'
    $files = @{}
    Compare-Object (gci $folderA) (gci $folderB) -Property Name, LastWriteTime |
    % { 
      if ($files[$_.Name] -eq $null) {
        $files[$_.Name] = New-Object PSObject -Property @{FolderA = ''; FolderB = ''; TimeStampA = $null; TimeStampB = $null}
      if ($_.SideIndicator -eq "=>") {
        $files[$_.Name].FolderA    = $folderA + '\' + $_.Name
        $files[$_.Name].TimeStampA = $_.LastWriteTime
      } else {
        $files[$_.Name].FolderB    = $folderB + '\' + $_.Name
        $files[$_.Name].TimeStampB = $_.LastWriteTime
    $files.GetEnumerator() | select -ExpandProperty Value | select FolderA, TimeStampA, FolderB, TimeStampB

    Open in new window

    But, honestly, I for myself would prefer an output looking like
    filename   inA   TimeStampA            inB    TimeStampB
    --------   ---   ----------            ----   ------------
    tst1        x    01/01/2000 01:02:03   
    tst2        x    01/01/2000 01:02:03    x     01/02/2000 03:04:05

    Open in new window

    as repeating the file path for each file does not add any information.

    Author Comment

    Qlemo, you are amazing!  That scripts works way better than I ever could have imagined!

    Thank you!  Thank you!

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
    This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now