Posted On January 4, 2020

PowerShell: Comparing 2 Directories

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Comparing 2 Directories
# Set source and destination
$source="C:\temp"
$destination="C:\tempcopy"

# Mirror the 2 directories
robocopy $source $destination /MIR /R:0 /NP

# create a test file in source to simulate a difference
$fileName = "testFile-"+(Get-Date).tostring("dd-MM-yyyy")
New-Item -itemType File -Path $source -Name ($fileName + ".txt")

# Collect child items of the 2 directories
$sourceItems=Get-ChildItem $source -Recurse -Force
$destinationItems=Get-ChildItem $destination -Recurse -Force

# Compare the 2 directories using the super slow SHA256 encryption algorithm (please don't run this on 1 million files as that take forever)
$sourceHashes=$sourceItems|% {Get-FileHash -Path $_.FullName}
$destinationHashes=$destinationItems|% {Get-FileHash -Path $_.FullName}
$fileDifferences=(Compare-Object -ReferenceObject $sourceHashes -DifferenceObject $destinationHashes -Property hash -PassThru).Path|Out-String

# Output the differences
write-host $fileDifferences

Sample Result:

PS C:\Windows\system32> $fileDifferences
C:\tempcopy\helloworld - Copy.py
C:\tempcopy\helloworld.py
C:\temp\helloworld.pp

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Start All Automatic Services on Windows

Current version: # startAllAutoServices.ps1 # version 0.0.2 # Description: # - This script to scan…

WordPress NextGen Gallery Plugin Error

Error Message: Failed to load plugin url: /bitnami/wordpress/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/attach_to_post/static/ngg_attach_to_post_tinymce_plugin.js?ver=3.17 Resolution: Although the root cause hasn't been…

Install Files within the NewPCSetupFiles Template Directory

net use y: \\FILESERVER01.kimconnect.com\users\template\newPCSetup y: regedit.exe /s disable_uac_win7.reg cd NewPCSetupFiles dotnetfx-winfsc.exe /Q dotNetFx40_Full_x86_x64.exe /quiet /norestartdotnetfx45_full_x86_x64.exe…