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,974 times
Add to favorites
E-mail Twitter Digg Facebook
  • What if there are lookups?
    1 Posts | Last post May 24, 2018
    • I'm trying to copy a library to a different site. Without lookup columns it is fine, but how to do if there is a lookup to another list? THIS list is not the same as the new one, so the lookups don't match or even throw an error if the item is not found.
      SiteA has DocLib1 with a lookup to List1
      SiteB has a DocLib2 with a lookup to List2
      List1 has been migrated to List2, the data matches, but the IDs don't.
      Now when you try to copy DocLib1 to DocLib2, it takes the lookupIds of List1 but applies them to List2.
      Got the point?
      The problem is in $DocLib2.RootFolder.Files.Add($url, $bytes, $true). 
      I tried to add the hashtab:
      $props = @{}
      $props.Add("Lookup", $theCorrectId)
      $DocLib2.RootFolder.Files.Add($url, $bytes, $props, $true)
      but the props is ignored.
  • Thanks
    1 Posts | Last post May 24, 2018
  • Error when Field name has hyphen in it
    2 Posts | Last post October 02, 2017
    • This is great - I needed to do exactly this!
      However, I've modified the script to include specific property fields that I need to move with the document.  I'm running into an issue because my field name's have a hyphen in them.
      $dFile.AddProperty($Field.Company-Type-DocumentType, $TargetItems.Properties[$Field.Company-Type-DocumentType])
      You must provide a value expression on the right-hand side of the '-' operator.
      At D:\temp\CopyFilesAndFoldersOrig.ps1:163 char:36
      +             $dFile.AddProperty($Field.Company- <<<< Type-DocumentType, $Ta
          + CategoryInfo          : ParserError: (:) [], ParseException
          + FullyQualifiedErrorId : ExpectedValueExpression
      Do you know how to get around this?
      Thank you for your help!
    • Possibly "a day late and a dollar short", but it sounds like it's interpreting the - as starting a parameter name.  Try escaping it (`-).
  • How to copy document from test server to production using powershell
    2 Posts | Last post May 29, 2017
    • Anyone help for this.
      How to copy SharePoint document library from test server to production using powershell
    • The URL does exists coul dyou please share the codes.
  • Sharepoint to file system
    2 Posts | Last post May 29, 2017
    • I am wondering if any one knows how to use this script to copy files, with custom metadata fields, to a file system.  Any help / advice is greatly apprecited
    • I had also same issues.Any body suggest what needs to be replace code
  • Does Not maintain the metadata
    4 Posts | Last post May 29, 2017
    • This script executes and copies all the files in the library which are NOT it folders. But I'm having two problems:
      (1) the metadata is not maintained. The newly-copied files have the current date/time and 'Modified by' is the admin account I used to run the script.
      (2) The one folder in the source library does not get copied over, nor are the files inside.
      I'm seeing multiple errors, the first being:
        Remove-Variable : Cannot find a variable with name 'ParentFolderURL'.
      the next is
        Exception calling "Update" with "0" argument(s): "Invalid file name.
      Followed by a string of "Cannot call a method on a null-valued expression." errors.
      My folder name doesn't have special characters in it, but there are spaces. 
      Any ideas for either of these issues?
      Thank you.
    • Even I got the issue with metadata. Kindly suggest
    • Not sure if you guys resolved the issue with the Remove-Variable ParentFolderURL error, but I got it to work by replacing that line with this:
      if(Test-Path variable:global:ParentFolderURL)
      Remove-Variable ParentFolderURL
    • Hi,
      I have also same issues not able to copy folder structure and original folder structure.Please help.
  • Copy SharePoint Files but not Folders
    3 Posts | Last post April 12, 2017
    • Is it possible to only copy the files without the folders?
    • For just the files, and not the folders, you would use $SPList.allfiles instead of using $SPFolder.files.  It would significantly reduce the length of the script if that was what you wanted to do.
    • This is nice, but the only missing part is that the files copying without the read only metadata fields. Is it possible for you to modify the script and provide us the script with copying the files with metadata and read only metadata as Created By,Created Date,etc.
      It willbe more helpful. 
  • Does not maintain metadata
    1 Posts | Last post February 22, 2017
    • any update on why it does not maintain metadata?
  • Traversing only one folder structure
    2 Posts | Last post October 21, 2016
    • Hi Roger,
      I've implemented the changes from the comment section, as well as the changes needed to write the source files into a single RootFolder in the destination library--I convinced the users to get rid of sub-folders any use metadata.  The script completes without any errors.  The issue I am running into is it is only the files from one folder path that is being copied:
      Source Library
      -2014 (folder)
         - 1 (folder)
            - file.2014.1.1 (file)
            - file.2014.1.n (file)
         - 2 (folder)
            - file.2014.2.1 (file)
            - file.2014.2.n (file)
         - 3 (folder)
            - file.2014.3.1 (file)
            - file.2014.3.n (file)
         - file.2014.1 (file)
         - file.2014.n (file)
      - 2015 (folder)
         - file.2015.1
         - file.2015.n
      - 2016 (folder)
         - 1 (folder)
            - file.2016.1.1 (file)
            - file.2016.1.n (file)
         - 2 (folder)
            - file.2016.2.1 (file)
            - file.2016.2.n (file)
         - 3 (folder)
            - file.2016.3.1 (file)
            - file.2016.3.n (file)
         - file.2016.1 (file)
         - file.2016.n (file)
      - Restore (folder)
         - Restore.1 (file)
         - Restore.n (file)
      Only the files in the 2016 folder structure are transferred to the destination library.  The files in the 2014, 2015 and Restore folder structures didn't get copied over to the destination.  What am I missing?  Thanks!
    • Call off the Marines--the files that didn't come over where checked out.  Once the files where checked in, and the script was re-run, all the files came over.
  • My Fixes
    2 Posts | Last post October 20, 2016
    • Hi,
      I had the problems that other users mentioned and I think I fixed it, the changes are:
      Change this:
      Remove-Variable ParentFolderURL
      To This:
          $varExists = Get-Variable ParentFolderURL -ErrorAction SilentlyContinue
          if($varExists -ne $null){
                 Remove-Variable ParentFolderURL  }
      Change This:
      $Relative = ($Item.ServerRelativeUrl).substring(1)
      To This:
      $Relative = ($Item.Url)
      Still testing it, but worked for me.
    • Another alternative to testing the ParentFolderURL before removing it is:
      if(Test-Path variable:global:ParentFolderURL)
      Remove-Variable ParentFolderURL
1 - 10 of 27 Items