top of page

Copy Documents From One SharePoint Document Library To Another Based On Created Date

In this article you will learn how to copy documents from one SharePoint Document Library to another based on created Date using PowerShell.

Welcome to an article on how to copy documents from one SharePoint Document library to another based on Created Date using PowerShell.

I searched the web everywhere and didn’t find this script so I thought of writing it down for my readers. Sometimes we need some of our files in another document library based on some interval of period from the date it was created.

You have a choice, using this script which will move the files based on your input as in from how many days from today you want the files which were created or uploaded during this time zone.

How? Let’s see it.

1. Open Windows PowerShell Modules as an Administrator.


Call the Web

  1. $web = Get-SPWeb "Add your web collection here"

Get the library

  1. $list = $web.Lists["Add your library name here"]  

  2. $spQuery = New-Object Microsoft.SharePoint.SPQuery  

  3. $spQuery.ViewAttributes = "Scope='Recursive'";  

  4. $spQuery.RowLimit = 2000  

Get the Current Date here

  1. $a = Get-Date   

Add the date from when you want the photos to be pulled as in from today until how many last days

  1. $twoday = $a.AddDays(-4)  $v = Get-Date $twoday -Format s  

Query the list

  1. Write - Host $v  

  2. $caml = '<Where><Gt><FieldRef Name="Created" /><Value Type="DateTime">' + $v + '</Value></Gt></Where>'

  3. $spQuery.Query = $caml  

  4. do {      

  5. $listItems = $list.GetItems($spQuery)      

  6. $spQuery.ListItemCollectionrank = $listItems.ListItemCollectionrank      

  7. # Get the count of items queried according to our condition     

  8. $listTotal = $listItems.Count      

  9. Write - Host $listTotal  

  10. for ($x = $listTotal - 1; $x - ge 0; $x--)      

  11. {  

  12. try         

  13. {              

  14. $listItems[$x].CopyTo("Add your library complete link here" + $listItems[$x].name)# It will run stating the name of the item              

  15. Write - Host("Updated: " + $listItems[$x].name)              

  16. $listItems[$x].Update()          

  17. }  

  18. catch         

  19. {             

  20.  Write - Host $_.Exception.ToString()          

  21. }      

  22. }  

  23. }  

  24. while ($spQuery.ListItemCollectionrank - ne $null)  

2. Save the above code in a notepad as .ps1 file.

3. Run the code on the Windows PowerShell Modules.

4. It will display the number of files moved from the number of days selected.

5. As I have updated 4 days, so it will move all the files which have been created during the last 4 days. It will be so quick that you don’t take much time and effort on this, thereby saving a lot of time and effort. Run the script and you have all the files moved at once.



bottom of page