Copy all SharePoint Files and Folders Using PowerShell

This script uses PowerShell to copy all files and folders from one document library to another document library.  This script will allow you to copy an entire document library, along with user-defined metadata from one site collection to another.

4.4 Star
14,743 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • Doesn't support site collections not at the root
    3 Posts | Last post October 29, 2014
    • The folder copy doesn't work if the site collection where the list lives is not the root site collection. 
      The following returns a server relative URL:
      $Relative = ($item.ServerRelativeUrl).SubString(1)
      But the Where clause uses site collection relative URL:
      $TargetItem = $AllFiles | ? {$_.URL -eq $Relative}
    • Thank-you for pointing this out in detail.  Working through modifying this for sharepoint 2013 and to allow it to copy from any sharepoint location to any other sharepoint location.  I was able to clear up some problems by replacing 
      $Relative = ($item.ServerRelativeUrl).SubString(1)
      $Relative = ($item.URL).SubString(1)  
      This cleared up some errors, but not quite there yet, still getting an error later in the script I suspect due to the paths used when writing the new files.
    • Next error was due to the exact same root cause identified above;  
      Took this line:  $dfile = $dList.RootFolder.Files.Add($RootItem.Name, $sBytes, $true)
      and changed to:  $dfile = $dList.RootFolder.Files.Add($Relative, $sBytes, $true)
      I suppose the errors probably have nothing to do with SP13, seems it didn't work for many people right out of the gate.  Anywho, this seems to have fixed it up. 
  • Same site collection
    3 Posts | Last post September 29, 2014
    • If I'm trying to move documents from one doc lib to another within the same spweb, is there anything else I need to change? I remove the destination web url and also I'm modifying the code to ignore the first folder in setting the parent url.
    • if you need to upload files, that you dowladed erlier,  you can usethis:
      if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
           Add-PSSnapin Microsoft.SharePoint.PowerShell;
      #Site Collection where you want to upload files
      $siteCollUrl = "http://webapplication/sites/Upload"
      #Document Library where you want to upload files
      $libraryName = "Shared Documents"
      #Physical/Network location of files
      $reportFilesLocation  = "C:\Users\manas\Desktop\Files"
      $spSourceWeb = Get-SPWeb $siteCollUrl;
      $spSourceList = $spSourceWeb.Lists[$libraryName];
      if($spSourceList -eq $null)
          Write-Host "The Library $libraryName could not be found."
      $files = ([System.IO.DirectoryInfo] (Get-Item $reportFilesLocation)).GetFiles()
      foreach($file in $files)
          #Open file
          $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
          #Add file
          $folder =  $spSourceWeb.getfolder($libraryName)
          Write-Host "Copying file $file to $libraryName..."
          $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
          #Close file stream
      Write-Host "Files have been uploaded to $libraryName."
    • i find it here
  • move files?
    1 Posts | Last post April 07, 2014
    • Hi Roger, 
      Great script! Wondering how to modify to select and move documents within the same site to a different document library based on a modified date range. Eg, I'd like to archive all files from 2008 to 2010 and also retain their user added tags/data in the columns. Open in explorer loses this value.
  • Cannot call a method on a null-valued array?
    2 Posts | Last post December 19, 2013
    • Hi Roger
      This looks great and exactly what I need but I am running into problems when running the script. The script creates the folder structure but does not copy the files within the folders. I am getting the following errors:
      Any ideas what I may be doing wrong?
      You cannot call a method on a null-valued expression.
      At line:46 char:50
      +             $sBytes = $TargetItem.File.OpenBinary <<<< ()
          + CategoryInfo          : InvalidOperation: (OpenBinary:String) [], RuntimeException
          + FullyQualifiedErrorId : InvokeMethodOnNull
      You cannot call a method on a null-valued expression.
      At line:48 char:49
      +             $dFile = $Newfolder.Folder.Files.Add <<<< ($TargetItem.Name, $sBytes, $true)
          + CategoryInfo          : InvalidOperation: (Add:String) [], RuntimeException
          + FullyQualifiedErrorId : InvokeMethodOnNull
      Cannot index into a null array.
      At line:55 char:43
      +                 if($TargetItem.Properties[ <<<< $Field.Title])
          + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
          + FullyQualifiedErrorId : NullArray
      You cannot call a method on a null-valued expression.
      At line:71 char:26
      +             $dFile.Update <<<< ()
          + CategoryInfo          : InvalidOperation: (Update:String) [], RuntimeException
          + FullyQualifiedErrorId : InvokeMethodOnNull
    • I have the same issue. Any ideas?
  • Thanks Roger
    1 Posts | Last post November 11, 2013
    • Thanks so much for the script, it’s great. 
      I have modified it to cater for multiple library migrations referencing a CSV file. If interested let me know
  • Copy SharePoint Files based on Criteria
    2 Posts | Last post February 13, 2013
    • I would like to copy all files in one Document Library in one site collection to another Document Library in another Site Collection based on the creation date.
      For example copy all documents created in 2008.
      Is this possible?
      I did create a view in the source DL that shows only documents from 2008, so if your script could use a view that could be an answer.
    • It would take a slight modification to the script. The original version of the script, before it was uploaded, did just that. I found that to be a bug for two reasons.  First, it could be prone to file name conflicts and overwriting files if somebody had two files with the same name in two separate folders.  Second, it really didn't make a "copy" of a document library.
  • Copy to external farm ???
    2 Posts | Last post November 22, 2012
    • Hi Roger
      Just a short question:
      Is it possible to set the DestinationUrl to the url of a website on the internet
      I mean, where do you set the credentials if the library do not have anonymous access ??
      Sorry if its too basic ;-)
      / geert
    • Not in the current implementation.  This script currently uses the local server OM in order to perform the copy.  It could however be rewritten to use a web service instead; however that could be a pretty challenging exercise.
21 - 27 of 27 Items