Copy Files Between Document Libraries in SharePoint using PowerShell

Requirement: Copy Files between SharePoint Document Libraries using PowerShell

copy files between document libraries using powershell

PowerShell Script to Copy a File from One Library to Another

In this blog post, we will show you how to copy files between SharePoint document libraries using PowerShell. This can be useful if you want to quickly copy or move files between two or more document libraries for backup or archiving purposes.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables for Processing
$WebURL="https://portal.crescent.com/sites/Sales/"
$SourceFile="https://portal.crescent.com/sites/Sales/TeamDocs/InvoiceTemp.xlsx"
$TargetLibrary="Invoice Documents"

#Get Objects
$Web = Get-SPWeb $WebURL
$SourceFile = $Web.GetFile($SourceFile)
$TargetLibrary = $Web.GetFolder($TargetLibrary)

#Copy the file into the Target library
$File = $TargetLibrary.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(), $true)

#Copy Meta-Data
$Item = $File.Item
$item["Created"] = $SourceFile.TimeCreated.ToLocalTime()
$item["Modified"] = $SourceFile.TimeLastModified.ToLocalTime()
$item["Author"] = $SourceFile.Author
$item["Editor"] = $SourceFile.ModifiedBy
#Update
$Item.UpdateOverwriteVersion()

PowerShell Script to Copy All Files Between Document Libraries:

Let’s copy all files and folders from one library to another along with their meta-data (excluding: the above!)

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Custom Function to Copy Files from Source Folder to Target
Function Copy-Files($SourceFolder, $TargetFolder)
{
    write-host "Copying Files from:$($SourceFolder.URL) to $($TargetFolder.URL)"
    #Get Each File from the Source 
    $SourceFilesColl = $SourceFolder.Files

    #Iterate through each item from the source
    Foreach($SourceFile in $SourceFilesColl) 
    {
        #Copy File from the Source
        $NewFile = $TargetFolder.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(),$True)
 
        #Copy Meta-Data from Source
        Foreach($Field in $SourceFile.Item.Fields)
        {
            If(!$Field.ReadOnlyField)
            {
                if($NewFile.Item.Fields.ContainsField($Field.InternalName))
                {
                    $NewFile.Item[$Field.InternalName] = $SourceFile.Item[$Field.InternalName]
                }
            }
        }
        #Update
        $NewFile.Item.UpdateOverwriteVersion()
    
        Write-host "Copied File:"$SourceFile.Name
    }
    
    #Process SubFolders
    Foreach($SubFolder in $SourceFolder.SubFolders)
    {
        if($SubFolder.Name -ne "Forms")
        {
            #Check if Sub-Folder exists in the Target Library!
            $NewTargetFolder = $TargetFolder.ParentWeb.GetFolder($SubFolder.Name)
 
            if ($NewTargetFolder.Exists -eq $false)
            {
                #Create a Folder
                $NewTargetFolder = $TargetFolder.SubFolders.Add($SubFolder.Name)
            }
            #Call the function recursively
            Copy-Files $SubFolder $NewTargetFolder
        }
    }
}

#Variables for Processing
$WebURL="https://portal.crescent.com/sites/sales/"
$SourceLibrary ="Team Docs"
$TargetLibrary = "Sales Documents"

#Get Objects
$Web = Get-SPWeb $WebURL
$SourceFolder = $Web.GetFolder($SourceLibrary)
$TargetFolder = $Web.GetFolder($TargetLibrary)

#Call the Function to Copy All Files
Copy-Files $SourceFolder $TargetFolder

Copy Files Between Folders using PowerShell

Want to copy files between Folders in document libraries? Simply change the parameter values $SoureLibrary and $TargetLibrary to Folder URLs. E.g.

#Relative Path of the Folder
$SourceLibrary ="/Documents/2017"
$TargetLibrary = "/Team Documents/Active/2018"

Copy Documents between Sites, Site Collections, Web Applications:

How about copying between Sites? Well, just change the below lines in the script.

#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

To move files between SharePoint document libraries, use: Move Files Between Document Libraries with Metadata and Version History

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

18 thoughts on “Copy Files Between Document Libraries in SharePoint using PowerShell

  • I’m so glad I came across this script. Your site is my favorite go-to for SharePoint issues. I’m running this script to move files between site collections and it gives me:
    Copying Files from:DocCentre/17CaseFiles/000000 Test Folder to sites/ClientFilesArchive/Client Files Archive/0000TestFolderShared Documents

    But no files has been moved to the target folder, it still is empty

    This is my variables:
    #Variables for Processing
    $SourceWebURL = “http://Myweb-app/”
    $TargetWebURL = “http://Myweb-app/”

    $SourceWeb = Get-SPWeb $SourceWebURL
    $TargetWeb = Get-SPWeb $TargetWebURL

    $SourceLibrary =”DocCentre/17CaseFiles/000000 Test Folder”
    $TargetLibrary = “sites/ClientFilesArchive/Client%20Files%20Archive/0000TestFolderShared Documents”

    What am I doing wrong?

    Reply
    • $TargetWebURL is the URL of the target web (Site collection or subsite, but not the web app URL). The $SourceLibrary should be “Site Relative Path” of the Folder.

      Reply
  • This dosn’t work if there are more than 5000 items in a document library. Any idea how to fix this?

    Reply
  • I want to Transfer files under multiple subfolders(f1,f2,f3) to another folder(f4) all under the same Document Library

    Reply
  • please i want to use this script to move folders with their subfolders and files but not copy as it dit in this script. How can I do? Thank you in advance.

    Reply
  • The logic to move folders along with files was very helpful. I was having hard time finding a correct logic to move the folders between two doc libraries and your solution assisted me. Thanks a ton.

    Reply
  • Would appreciate if someone can give me a hand update the script so I can specify the source and target folders. Eg. I have Folder A and Folder B on the same library and want to move documents in between folders.

    Ramsey

    Reply
    • It’s fairly simple. Just use the script in “PowerShell Script to Copy All Files Between Document Libraries” section and set the variables according to your environment!

      Reply
  • Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

    #Custom Function to Copy Files from Source Folder to Target

    Function Copy-Files($SourceFolder, $TargetFolder)

    {

    write-host “Copying Files from:$($SourceFolder.URL) to $($TargetFolder.URL)”

    #Get Each File from the Source

    $SourceFilesColl = $SourceFolder.Files

    #Iterate through each item from the source

    Foreach($SourceFile in $SourceFilesColl)

    {

    #Copy File from the Source

    $NewFile = $TargetFolder.Files.Add($SourceFile.Name, $SourceFile.OpenBinary(),$True)

    #Copy Meta-Data

    $Item = $NewFile.Item

    $item[“Created”] = $SourceFile.TimeCreated.ToLocalTime()

    $item[“Modified”] = $SourceFile.TimeLastModified.ToLocalTime()

    $item[“Author”] = $SourceFile.Author

    $item[“Editor”] = $SourceFile.ModifiedBy

    #Update

    $Item.UpdateOverwriteVersion()

    Write-host “Copied File:”$SourceFile.Name

    }

    #Process SubFolders

    Foreach($SubFolder in $SourceFolder.SubFolders)

    {

    if($SubFolder.Name -ne “Forms”)

    {

    #Check if Sub-Folder exists in the Target Library!

    $NewTargetFolder = $TargetFolder.ParentWeb.GetFolder($SubFolder.Name)

    if ($NewTargetFolder.Exists -eq $false)

    {

    #Create a Folder

    $NewTargetFolder = $TargetFolder.SubFolders.Add($SubFolder.Name)

    }

    #Call the function recursively

    Copy-Files $SubFolder $NewTargetFolder

    }

    }

    }

    #Variables for Processing

    $SourceWebURL = “https://sp16vm001:33333/kunal/”

    $TargetWebURL = “https://sp16vm001:11111/”

    $SourceWeb = Get-SPWeb $SourceWebURL

    $TargetWeb = Get-SPWeb $TargetWebURL

    $SourceLibrary =”Testlib”

    $TargetLibrary = “Testlib”

    $SourceFolder = $SourceWeb.GetFolder($SourceLibrary)

    $TargetFolder = $TargetWeb.GetFolder($TargetLibrary)

    #Call the Function to Copy All Files

    Copy-Files $SourceFolder $TargetFolder

    kunal

    Reply
  • Hi, Could you please tell how to delete the file from source after copying to the destination. I have a number of list which is reading through a csv file to get the where value and destination library. I’ve managed to get the copy file but deleting the file in the same loop is giving me an error. collection was modified;enumeration operation may not execute. Can we do the copy and delete in the same script. Anyhelp would be appreciated.
    Thanks

    Reply
  • I was working on this script for a day now until realized, that this is exactly what i need.
    To get it working for me, I had to change some lines:

    $SourceWeb = “https://Your-Source-Web-URL” —> $SourceWeb = GetWeb(“https://Your-Source-Web-URL”)
    ..
    $SourceFolder = $SourceWeb.GetFolder($SourceLibrary) —> $SourceFolder = $SourceWeb.getList[$SourceLibrary].RootFolder
    ..

    Also the script did not manage, to copy the given meta data for the folders given in the source library. But that’s a minor problem.
    Very useful script for me. Thanks alot!

    Reply
  • Hi,

    Here is a free open-source tool that can do that and a whole lot more:
    https://github.com/GLubomirov/SP_Hauler/releases/tag/1.0

    Greetings,
    George

    Reply
  • Thanks for this great script. But there is a syntax error with the script.

    $SourceFolder = $Source.GetFolder($SourceLibrary) –> $SourceFolder = $SourceWeb.GetFolder($SourceLibrary)

    Reply
  • Nice Script can we only copy folder with Data to Different Library ?

    Reply
  • Awesome. Thank you so much. I was looking for something to copy between On-prem and Online, pretty much just as a file dump from A to B. This worked like a charm.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *