kwizcom banner advertisement

SharePoint Online: Copy File between Document Libraries using PowerShell

How to Copy a File in SharePoint Online Document Library?
  • Navigate to your SharePoint Online document library. Right click on the file to copy >> Select "Copy To" menu item
    sharepoint online copy document
  • This opens information panel in the right. Select the target library to which your needs to be copied. You can select current library, any other library in the current site or even a library in any different site collection.
    sharepoint online copy file
  • Pick the library and click on "Copy Here" button to start copying the file.
  • You'll see the "Copying" message in tool bar and your fill will be copied momentarily.
    sharepoint online powershell copy documents
SharePoint Online: PowerShell to Copy Documents
Here is the PowerShell to copy files in SharePoint Online document library.
Function Copy-File
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $SourceFileURL,
        [Parameter(Mandatory=$true)] [string] $TargetFileURL
    )
    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the source file
        $SourceFile =$Ctx.Web.GetFileByServerRelativeUrl($SourceFileURL)
        $Ctx.Load($SourceFile)
        $Ctx.ExecuteQuery()

        #Copy File to destination
        $SourceFile.CopyTo($TargetFileURL, $True)
        $Ctx.ExecuteQuery()

        Write-Host "File Copied from '$SourceFileURL' to '$TargetFileURL'" -F Green
       }
    Catch {
        write-host -f Red "Error Copying File!" $_.Exception.Message
    }
}

#Set Parameter values
$SiteURL="https://crescent.sharepoint.com/"
$SourceFileURL="/Project Documents/Active Users.xlsx"
$TargetFileURL="/Project Documents/Active Users2.xlsx"

#Call the function 
Copy-File -SiteURL $SiteURL -SourceFileURL $SourceFileURL -TargetFileURL $TargetFileURL 
This method copies given document either to same document library or different document library with in the same site along with its metadata fields (Except: Created by and Modified by fields - and obviously, the columns should exists in both libraries)!

Copy All Files from One Document Library to Another using PowerShell:
This time, lets copy all files along with its folder-subfolder structure
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Copy-AllFiles
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
    )
    Try {
        #Get all Files from the source folder
        $SourceFilesColl = $SourceFolder.Files
        $Ctx.Load($SourceFilesColl)
        $Ctx.ExecuteQuery()

        #Iterate through each file and copy
        Foreach($SourceFile in $SourceFilesColl)
        {
            #Get the source file
            $SourceFile =$Ctx.Web.GetFileByServerRelativeUrl($SourceFile.ServerRelativeUrl)
            $Ctx.Load($SourceFile)
            $Ctx.ExecuteQuery()
            
            #Copy File to destination
            $TargetFileURL = $TargetFolder.ServerRelativeUrl+"/"+$SourceFile.Name
            $SourceFile.CopyTo($TargetFileURL, $True)
            $Ctx.ExecuteQuery()

            Write-host -f Green "Copied File '$($SourceFile.ServerRelativeUrl)' to '$TargetFileURL'"
        }

        #Process Sub Folders
        $SubFolders = $SourceFolder.Folders
        $Ctx.Load($SubFolders)
        $Ctx.ExecuteQuery()
        Foreach($SubFolder in $SubFolders)
        {
            If($SubFolder.Name -ne "Forms")
            {
                #Prepare Target Folder
                $TargetFolderURL = $SubFolder.ServerRelativeUrl -replace $SourceLibrary.RootFolder.ServerRelativeUrl, $TargetLibrary.RootFolder.ServerRelativeUrl
                Try {
                        $Folder=$Ctx.web.GetFolderByServerRelativeUrl($TargetFolderURL)
                        $Ctx.load($Folder)
                        $Ctx.ExecuteQuery()
                    }
                catch {
                        #Create Folder
                        if(!$Folder.Exists)
                        {
                            $Folder=$Ctx.Web.Folders.Add($TargetFolderURL)
                            $Ctx.Load($Folder)
                            $Ctx.ExecuteQuery()
                            Write-host "Folder Added:"$SubFolder.Name -f Yellow
                        }
                    }
                #Call the function recursively
                Copy-AllFiles -SiteURL $SiteURL -SourceFolder $SubFolder -TargetFolder $Folder
            }
        } 
    }
    Catch {
        write-host -f Red "Error Copying File!" $_.Exception.Message
    }
}

#Set Parameter values
$SiteURL="https://crescent.sharepoint.com/"
$SourceLibraryName="Project Documents"
$TargetLibraryName="Documents"

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
     
#Get the source library and Target Libraries
$SourceLibrary = $Ctx.Web.Lists.GetByTitle($SourceLibraryName)
$Ctx.Load($SourceLibrary)
$Ctx.Load($SourceLibrary.RootFolder)
$TargetLibrary = $Ctx.Web.Lists.GetByTitle($TargetLibraryName)
$Ctx.Load($TargetLibrary)
$Ctx.Load($TargetLibrary.RootFolder)
$Ctx.ExecuteQuery()


#Call the function 
Copy-AllFiles -SiteURL $SiteURL -SourceFolder $SourceLibrary.RootFolder -TargetFolder $TargetLibrary.RootFolder
SharePoint Online: Copy File between Document Libraries using PowerShell SharePoint Online: Copy File between Document Libraries using PowerShell Reviewed by Salaudeen Rajack on February 04, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.