none
Powershell script to compare contents of two derectories RRS feed

  • Question

  • I am looking to do a powershell script to compare files in two different directories and return that information in a report form list the files that are different. The report must include file name, date, size and location. I want more information than just the file name and an =>. I am hoping to format the report like:

    The following files in the local server (excaliber) are newer than, different size than or are missing on the remote server (irnearline2)

    Name                           Date                                    Length                             Location
    --------                         -------------                          --------------------             -------------------
    File1.zip                       01/21/2012                          1234567                           \\excaliber.....
    File2.zip                       01/21/2012                          1234567                           \\excaliber.....
    File3.zip                       01/21/2012                          1234567                           \\excaliber.....
    File4.zip                       01/21/2012                          1234567                           \\excaliber.....

    What I have so far:

    <#
    This script gets information from the local side folder and compares it to the remote side
    It then sends a report to John
    This script was created on 10.7.16
    This script was created by Brad Tostenson
    #>
    $original = Get-ChildItem -Recurse -Path "\\excaliber\FTP\iPartners\Backup"
    $backup = Get-ChildItem -Recurse -Path "\\irnearline2\SANBackup\iPartners Backup\Backup"
    Compare-Object -ReferenceObject $original -DifferenceObject $backup -Property Name,Length

    Friday, October 7, 2016 8:11 PM

Answers

  • Wheel re-invented for my own purposes and it seems to work for me
    This is what I came up with:

    <#
    This script gets information from the original folder and compares it to the backup side and emails the result
    This script was created on 10.10.16
    This script was created by Brad Tostenson
    #>
    # sets the filename to be created based on the date along with location to create it
    $fileName = (Get-Date -Format yyMMdd) + ".txt"
    $location = "C:\backup\"
    # sets up the two folders to be compared
    $original = Get-ChildItem -Recurse -Path "\\excaliber\FTP\iPartners\Backup"
    $backup = Get-ChildItem -Recurse -Path "\\irnearline2\SANBackup\iPartners Backup\Backup"
    # tests the outcome of the comparison. if the compare comes back with nothing, the script ends
    $outcome = Compare-Object -ReferenceObject $original -DifferenceObject $backup -Property Name,Length
    if($outcome -eq $null)
    {exit}
    # creates the file to populate with results of the compare
    New-Item -ItemType file -Name $fileName -Path $location
    # compares the two folders original and backup and populates the files
    Compare-Object -ReferenceObject $original -DifferenceObject $backup -Property Name,Length | Out-File -FilePath (Join-Path $location $fileName)
    # Sends an email to recipient with the results
    Send-MailMessage -To "emailaddress"  -From "emailaddress" -Subject "iPartner Backup" -Attachments "c:\backup\$filename" -Body "See Attachment" -BodyAsHtml -SmtpServer smtpserver

    • Marked as answer by b2netman Monday, October 10, 2016 7:05 PM
    Monday, October 10, 2016 7:05 PM

All replies

  • Why reinvent the wheel?

    http://www.google.com/search?&q=free+directory+compare+tool


    -- Bill Stewart [Bill_Stewart]

    Friday, October 7, 2016 8:42 PM
    Moderator
  • Right, there are a bunch of tools available. But if you need the data in PowerShell, comparing the file hash is the best option.

    $files = Get-ChildItem -Recurse -Path D:\1, D:\2 | Add-Member -Name Hash -MemberType ScriptProperty -Value { ($this | Get-FileHash).Hash } -PassThru
    
    $files | Group-Object -Property Hash |
    Sort-Object -Property FullName -PipelineVariable group |
    ForEach-Object { $_.Group } |
    Format-Table -Property Name, Length, FullName, LastWriteTime, @{ Name = 'File Count'; Expression = { $group.Count } }


    -Raimund

    Monday, October 10, 2016 2:31 PM
  • Wheel re-invented for my own purposes and it seems to work for me
    This is what I came up with:

    <#
    This script gets information from the original folder and compares it to the backup side and emails the result
    This script was created on 10.10.16
    This script was created by Brad Tostenson
    #>
    # sets the filename to be created based on the date along with location to create it
    $fileName = (Get-Date -Format yyMMdd) + ".txt"
    $location = "C:\backup\"
    # sets up the two folders to be compared
    $original = Get-ChildItem -Recurse -Path "\\excaliber\FTP\iPartners\Backup"
    $backup = Get-ChildItem -Recurse -Path "\\irnearline2\SANBackup\iPartners Backup\Backup"
    # tests the outcome of the comparison. if the compare comes back with nothing, the script ends
    $outcome = Compare-Object -ReferenceObject $original -DifferenceObject $backup -Property Name,Length
    if($outcome -eq $null)
    {exit}
    # creates the file to populate with results of the compare
    New-Item -ItemType file -Name $fileName -Path $location
    # compares the two folders original and backup and populates the files
    Compare-Object -ReferenceObject $original -DifferenceObject $backup -Property Name,Length | Out-File -FilePath (Join-Path $location $fileName)
    # Sends an email to recipient with the results
    Send-MailMessage -To "emailaddress"  -From "emailaddress" -Subject "iPartner Backup" -Attachments "c:\backup\$filename" -Body "See Attachment" -BodyAsHtml -SmtpServer smtpserver

    • Marked as answer by b2netman Monday, October 10, 2016 7:05 PM
    Monday, October 10, 2016 7:05 PM