SharePoint Online: Copy a Document Library using PowerShell

Requirement: PowerShell to Copy a Document Library in SharePoint Online

How to Copy a Document Library in SharePoint Online?
Well, there are no direct ways to copy a document library in SharePoint Online. However, You can use this work-around:

  1. Save the source document library as a template - Go to the source library >> Click on Settings gear >> Choose Library Settings >> Click on "Save as Template". More info: How to Save List as Template in SharePoint Online?
  2. Create a new document library from the list template - Login to your SharePoint Online site >> Click on Settings gear icon and click "Add an app". You can use the pagination in the bottom of the page to find your custom list template, or use "Find an App" search box and pick your list template. More info: SharePoint Online: Create List from Custom Template using PowerShell
  3. Copy all items from source document library to the new document library - Once you created a new instance of the document library from existing list template, you can copy all files and folders from the source library to the destination library by selecting all items in the source library and choose "Copy to" button in the toolbar and then specify the document library created in step 2.
    sharepoint online powershell copy document library
Make sure you have custom script enabled for the site collection before proceeding with this method! How to Enable Custom Script in SharePoint Online?

SharePoint Online: PowerShell to Copy a Document Library
Let's automate the above steps to copy a document library in SharePoint Online using PowerShell:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$SourceLibraryName = "Documents"
$DestinationLibraryName = "DocumentsV2"

Try {
    #Connect to the Site
    Connect-PnPOnline -URL $SourceSiteURL -UseWebLogin

    #Get the Source library
    $SourceLibrary =  Get-PnPList -Identity $SourceLibraryName  -Includes RootFolder

    #Step 1: Save the  Source Library as a template
    $SourceLibrary.SaveAsTemplate($SourceLibrary.Title, $SourceLibrary.Title, "", $False)
    Invoke-PnPQuery 

    #Get the Library Template created
    $Ctx = Get-PnPContext
    $Web = Get-PnPWeb
    $RootWeb = $Ctx.Site.RootWeb
    $ListTemplates = $Ctx.Site.GetCustomListTemplates($RootWeb)
    $Ctx.Load($RootWeb)
    $Ctx.Load($ListTemplates)
    Invoke-PnPQuery
    $ListTemplate = $ListTemplates | Where {$_.Name -eq $SourceLibraryName}
 
    #Step 2:  Create the destination library from source library template
    If(!(Get-PnPList -Identity $DestinationLibraryName))
    {
        #Create the destination library
        $ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListCreation.Title = $DestinationLibraryName
        $ListCreation.ListTemplate = $ListTemplate
        $DestinationList = $Web.Lists.Add($ListCreation)
        Invoke-PnPQuery
        Write-host "New Library '$DestinationLibraryName' created successfully!" -f Green
    }
    Else
    {
        Write-host "Library '$DestinationLibraryName' already exists!" -f Yellow
    }
    #Remove the List Template
    Remove-PnPFile -SiteRelativeUrl ("/_catalogs/lt/"+$ListTemplate.InternalName) -Recycle -Force

    #Step 3: Copy content from Source to destination library
    $DestinationLibrary = Get-PnPList $DestinationLibraryName -Includes RootFolder
    $SourceLibraryURL = $SourceLibrary.RootFolder.ServerRelativeUrl
    $DestinationLibraryURL = $DestinationLibrary.RootFolder.ServerRelativeUrl

    #Copy All Content from Source Library to the Destination Library
    Copy-PnPFile -SourceUrl $SourceLibraryURL -TargetUrl $DestinationLibraryURL -SkipSourceFolderName -Force -OverwriteIfAlreadyExists
    Write-host "`tContent Copied from $SourceLibraryURL to  $DestinationLibraryURL Successfully!" -f Green
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
This PowerShell method retains all metadata except created/modified timestamps and created by, modified by field values.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.