kwizcom banner advertisement

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:
Here is my PowerShell script to copy files between SharePoint document libraries:
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:
Lets copy all files and folder 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="http://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 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 = "http://Your-Source-Web-URL"
$TargetWebURL = "http://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
Copy Files Between Document Libraries in SharePoint using PowerShell Copy Files Between Document Libraries in SharePoint using PowerShell Reviewed by Salaudeen Rajack on 6:38 PM Rating: 5

6 comments:

  1. 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.

    ReplyDelete
  2. Nice Script can we only copy folder with Data to Different Library ?

    ReplyDelete
  3. Thanks for this great script. But there is a syntax error with the script.

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

    ReplyDelete
    Replies
    1. Yes! It should be $SourceWeb instead of $Source. Fixed it - Thanks Hasan!

      Delete
  4. 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

    ReplyDelete
  5. 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 = "http://Your-Source-Web-URL" ---> $SourceWeb = GetWeb("http://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!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.