Forum Discussion
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 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
Thanks for the help,
Michael Williams
10 Replies
- NanddeepNachanLearn Expert
The code here should help you to compare 2 files:
https://devblogs.microsoft.com/scripting/use-powershell-to-compare-two-files/
- mwilliams71Brass Contributor
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.
- Joao LivioIron Contributor
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
- mwilliams71Brass ContributorI 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 LivioIron 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