Forum Discussion

mwilliams71's avatar
mwilliams71
Brass Contributor
Oct 14, 2022

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

 

#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

    • mwilliams71's avatar
      mwilliams71
      Brass 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.

    • mwilliams71's avatar
      mwilliams71
      Brass 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's avatar
        Joao Livio
        Iron 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

         

Resources