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

CSS: Units of Measurement

Common Values of Measurement px :pixelin :inchesmm :millimetersem :relative to nearest parent element's set value…

Indications that Chocolatey is locked down

# System's version is less than vendor's current (Chocolatey v0.10.15) 0+000+00[LAX-WEB005]: PS C:\Users\testadmin\Documents> choco source…

Basic JavaScript: Manipulate Arrays With shift()

// Setupvar myArray = [["John", 23], ["dog", 3]];// Only change code below this line.var removedFromMyArray=myArray.shift();