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

Basic JavaScript: Manipulate Arrays With pop()

// Setupvar myArray = [["John", 23], ["cat", 2]];// Only change code below this line.var removedFromMyArray=myArray.pop();

IIS Mime Types

One of the features of IIS security is to enforce file access by its associated…

Show Users Logons per Computer Names

# This script is to obtain log on information from a set of target computernames…