Comparing Files in two Document Libraries with PowerShell

Contributor

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 any code is different in the first document library than the second document library, I would Like to run a PowerShell script to copy the changed files and over-write the files with the same name in the second document library.

 

I have this code so far from: Copy Files Between Document Libraries in SharePoint using PowerShell - SharePoint Diary

 

#Variables for Processing
$SourceWebURL = "https://Your-Source-Web-URL"
$TargetWebURL = "https://Your-Target-Web-URL"
 
$SourceWeb = Get-SPWeb $SourceWebURL
$TargetWeb = Get-SPWeb $TargetWebURL
 
$SourceLibrary ="Team Docs"
$TargetLibrary = "Shared Documents"
 
$SourceFolder = $SourceWeb.GetFolder($SourceLibrary)
$TargetFolder = $TargetWeb.GetFolder($TargetLibrary)
 
#Call the Function to Copy All Files
Copy-Files $SourceFolder $TargetFolder




 

Thanks for the help,

 

Michael Williams

10 Replies

Hi @mwilliams71 

 

Just an idea, in your case first I will download the stream, and by the Microsoft.PowerShell.Utility I'll compare both files.

 

Compare-Object (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn

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.

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

 

@NanddeepNachan- Thanks for this code. This may get me started, but I need to check if any file was changed in the directory.  If a file was modified in the library, copy that file to a library on a different web application.

If 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.
See link in first reply, have examples

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 <=

Hi @mwilliams71 

 

The SideIndicator simply tell you where is the difference is.

 

<= Difference is in Left side

first object to compare

=> Difference is in the Right side

Second object in the comparison

 

My first suggestion was to download, compare and upload.

 

You have to mix all suggestions and architect your outcome, or wait for more.

 

Good luck :thumbs_up: 

Does this compare the file name or the text in each file as well?