Forum Discussion
mwilliams71
Oct 14, 2022Brass Contributor
Comparing Files in two Document Libraries with PowerShell
I have 30 files with code in a library in a web application imbedded in sub folders that I want to compare to another 30 files with code in a library in a separate web application in sub folders. If ...
mwilliams71
Oct 14, 2022Brass Contributor
I want to automate this by using PowerShell and if any code is different in the multiple directories, PowerShell should copy what files have changed to the Library.
Joao Livio
Oct 14, 2022Iron Contributor
Hi mwilliams71
Here it is an example assuming only one site, please adapt and test because you want to compare a stream
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables for Processing
$SourceWebURL = "https://Your-Source-Web-URL"
$SourceLibrary ="Team Docs"
$TargetLibrary = "Shared Documents"
$ColumnToCompare="Your Compare Column Name"
$Web = Get-SPWeb $SourceWebURL
$ListOne = $web.lists[$SourceLibrary]
$ListTwo = $web.lists[$TargetLibrary]
$ListOneValues = @()
$ListTwoValues = @()
$ListOne.Items| foreach { $ListOneValues+= $_[$ColumnToCompare] }
$ListTwo.Items | foreach { $ListTwoValues+= $_[$ColumnToCompare] }
Compare-Object $ListOneValues $ListTwoValues
- mwilliams71Oct 17, 2022Brass ContributorIf the column "Modified" is different, what result will I get? and how will I store this into a variable?
This may work for initiating the copy file PowerShell script.- Joao LivioOct 17, 2022Iron ContributorSee link in first reply, have examples
- mwilliams71Oct 17, 2022Brass Contributor
I'm not sure how this will help me when comparing over 2,000 files in one library to another, so that the files can be exact duplicates. I would make changes in say Test Web Application in the asset library and these changes should be copied to the production web application in the asset library. It looks like the code below is only looking for words in one text file.
Compare-Object -ReferenceObject (Get-Content -Path C:\Test\Testfile1.txt) -DifferenceObject (Get-Content -Path C:\Test\Testfile2.txt)
InputObject SideIndicator
----------- -------------
cat =>
racoon =>
dog <=
squirrel <=