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

PowerShell: File Copying Operation

2/11/20 Update: Version 0.1.8 is available here: https://blog.kimconnect.com/powershell-file-copy-script-using-emcopy-vss-legacy/ Version 0.1.6 <#.Description File_Copy_Script Version: 0.16Purpose: this…

PowerShell: Microsoft Exchange Admin Reports

Function importExchangeModule{ $snapinLoaded = (get-pssnapin microsoft.exchange.management.* -ErrorAction SilentlyContinue).Name $exchangeVersion=(GCM Exsetup.exe | % {$_.FileVersionInfo}).ProductVersion $exchangeVersionMajor=$exchangeVersion.Substring(0,2); $exchangeVersionMinor=$exchangeVersion.Substring(3,2);…

Use PowerShell to Set Microsoft SQL Database Owner

$owner='CAP\SQL Admins' $databaseName='TestDb' $sqlServer=$env:computername function setDbOwner{ param( $principle=$env:USERDOMAIN+'\Domain Admins', $databaseName='TestDB', $sqlServer ) function includeSqlTools{ $ErrorActionPreference='stop'…
Other project: DragonCoin.com