Powershell script - Sharepoint online - Copy Library contents to another library in a different site & filter the files copied.

Nigel Foot 76 Reputation points
2022-05-23T23:09:00.387+00:00

Hi, I would like to get some help with creating a script. The powershell script needs to have the ability to sign in to sharepoint, have input prompts for site, site pages and library to copy from and to. I need to be able to copy library content files from said library to a different library in another site and on top of that filter the copied files by creator. (maybe the creators email address??)

I am a newbie I admit, I have searched and search for a script that does this and I have tried to get something that works but to no avail as yet as now I have a knowledge block! 😦

I would be grateful for the help and some knowledge insight.

Many thanks in advance.

I have made a start and this is what I have so far. (which deals with entering the information and login to sharepoint.

Script to copy files from one sharepoint library to another and filter the files that need to be copied by creator.

Do{

Source document library full path

$FullSiteUrl = "https://oursite"
$SourceURL = Read-Host "Enter the source Url - (EXAMPLE: sites/template/icon)"
$TargetURL = Read-Host "Enter the Target Url - (EXAMPLE: sites/365training)"
$libraryName = Read-Host "Enter the name the Library i.e. 'sitePages'"
$AuthorEmail = Read-Host "Enter the name of the Author of the files that you want to copy to the destination site"

Write-Host "The entered Source Tenant URL is" $FullSiteUrl -ForegroundColor Green
Write-Host "The entered Source url is" $SourceURL -ForegroundColor Green
Write-Host "The entered Target URL is" $TargetURL -ForegroundColor Green
Write-Host "The entered Library Name is" $libraryName -ForegroundColor Green
Write-Host "The entered Author is" $AuthorEmail -ForegroundColor Green

$Confirmation = Read-Host -Prompt "Is this correct? (y/n)"

}

while ($Confirmation -ne "y")

Connect To Site

Write-Host "Connecting to site" -ForegroundColor Yellow
Connect-PnPOnline -url $FullSiteUrl -Interactive
Write-Host "Connected" -ForegroundColor Green

Get-PnPListItem -List $libraryName | where {$.FieldValues.Author.Email -eq $AuthorEmail -and $.FileSystemObjectType -eq "File"}

$sourceFieldCol = $sourceList.FieldsValues;
$sourceItems = $sourceList
$var1 = $item.FieldValues.FileLeafRef

$destinationList = Get-PnPListItem -List $libraryName

Copy data$($item.FieldValues.FileLeafRef)

Copy-PnPFile -SourceUrl $SourceURL/$LibraryName -TargetUrl $TargetURL/$LibraryName

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,229 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Nigel Foot 76 Reputation points
    2022-05-25T01:01:25.697+00:00

    Hi, I have now created script that works for me. I thought I would share the solution with you.

    My Script is as follows:

    Start of Script

    Parameters

    Do{
    $SiteURL = Read-Host "Enter the full source Site Url (EXAMPLE: https://your-tenant/sites/template/icon)"
    $LibraryName = Read-Host "Enter the name the Library (EXAMPLE: 'sitePages)'"
    $AuthorEmail = Read-Host "Enter the name of the Author of the files that you want to copy to the destination site"
    $TargetUrl = Read-Host "Enter the Target Sites Url (EXAMPLE: sites/365Training/sitePages)"

    Confirmation

    Write-Host "The entered Source Tenant URL is:" $SiteUrl -ForegroundColor Green
    Write-Host "The entered Library Name is:" $LibraryName -ForegroundColor Green
    Write-Host "The entered Author is:" $AuthorEmail -ForegroundColor Green
    Write-Host "The entered Target URL is:" $TargetURL -ForegroundColor Green

    $Confirmation = Read-Host -Prompt "Is this correct? (y/n)"

    }

    while ($Confirmation -ne "y")

    Connect to PnP Online

    Write-Host "Connecting to site" -ForegroundColor Yellow
    Connect-PnPOnline -Url $SiteURL -Interactive
    Write-Host "Connected" -ForegroundColor Green

    Get all files created by a particular user from the Library

    $ListItems = Get-PnPListItem -List $Libraryname -PageSize 2000 | where {$.FieldValues.Author.Email -eq $AuthorEmail -and $.FileSystemObjectType -eq "File"}

    $Resultset = @()

    Collect documents data

    $ListItems | ForEach-Object {
    $Resultset += New-Object PSObject -Property ([Ordered] @{
    Name = $.FieldValues.FileLeafRef
    RelativeURL = $
    .FieldValues.FileRef
    CreatedBy = $.FieldValues.Author.Email
    CreatedOn = $
    .FieldValues.Created
    ModifiedBy = $.FieldValues.Editor.Email
    ModifiedOn = $
    .FieldValues.Modified
    FileSizeInKB = $_.FieldValues.File_x0020_Size
    })
    }

    Get Result set

    $Resultset
    foreach ($item in $ListItems) {}
    if ($item.FileSystemObjectType -eq "File"){
    Write-Host "Copying file: $($item.FieldValues.FileLeafRef)"
    Copy-PnPFile -SourceUrl "$($item.FieldValues.FileRef)" -TargetUrl "/$TargetUrl" -Force
    }

    Write-Host "Great, your Files have copied" -ForegroundColor Green

    Pause

    End of Script

    1 person found this answer helpful.

  2. Nigel Foot 76 Reputation points
    2022-05-26T06:08:00.697+00:00

    OK - So I realised when testing the script to copy multiple files that it only actually copied one file.

    Here is the amended working script that copies all files filtered by authors email address:

    Parameters
    Do{
    $SiteURL = Read-Host "Enter the full source Site Url (EXAMPLE: https://your-tenant/sites/template/icon)"
    $FolderServerRelativeUrl = Read-Host "Enter the Folder Path to copy from (EXAMPLE: /sites/template/icon)"
    $LibraryName = Read-Host "Enter the name the Library (EXAMPLE: 'sitePages)'"
    $AuthorEmail = Read-Host "Enter the name of the Author of the files that you want to copy to the destination site"
    $TargetUrl = Read-Host "Enter the Target Sites Url (EXAMPLE: (No /)sites/365Training/sitePages)"
    Confirmation
    Write-Host "The entered Source site URL is:" $SiteUrl -ForegroundColor Green
    Write-Host "The entered Library Name is:" $LibraryName -ForegroundColor Green
    Write-Host "The entered Author is:" $AuthorEmail -ForegroundColor Green
    Write-Host "The entered Target URL is:" $TargetURL -ForegroundColor Green
    Write-Host "The entered FolderServerRelativePath is:" $FolderServerRelativeUrl/$LibraryName -ForegroundColor Green
    $Confirmation = Read-Host -Prompt "Is this correct? (y/n)"
    }
    while ($Confirmation -ne "y")
    Connect to PnP Online
    Write-Host "Connecting to site" -ForegroundColor Yellow
    Connect-PnPOnline -Url $SiteURL -Interactive
    Write-Host "Connected - Please wait while your files are copied" -ForegroundColor Green
    $allItems = Get-PnPListItem -List "$LibraryName" -FolderServerRelativeUrl "$FolderServerRelativeUrl/$LibraryName" | where {$.FieldValues.Author.Email -eq $AuthorEmail -and $.FileSystemObjectType -eq "File"}
    foreach ($item in $allItems) {
    if ($item.FileSystemObjectType -eq "File") {
    Write-Host "Copying file: $($item.FieldValues.FileLeafRef)" -ForegroundColor green
    Copy-PnPFile -SourceUrl "$($item.FieldValues.FileRef)" -TargetUrl "/$TargetUrl" -Force
    }
    }
    Write-Host "Great, your Files have copied" -ForegroundColor Green
    

    Pause

    0 comments No comments

  3. Yi Lu_MSFT 17,611 Reputation points
    2022-06-06T08:51:13.493+00:00

    Hi @Nigel Foot
    Great to know that it works now and thanks for sharing the update here.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others.". and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    Issue Symptom:
    Hi, I would like to get some help with creating a script. The powershell script needs to have the ability to sign in to sharepoint, have input prompts for site, site pages and library to copy from and to. I need to be able to copy library content files from said library to a different library in another site and on top of that filter the copied files by creator. (maybe the creators email address?)

    Current status:
    Here is the amended working script that copies all files filtered by authors email address:

    Parameters
    Do{
    $SiteURL = Read-Host "Enter the full source Site Url (EXAMPLE: https://your-tenant/sites/template/icon)"
    $FolderServerRelativeUrl = Read-Host "Enter the Folder Path to copy from (EXAMPLE: /sites/template/icon)"
    $LibraryName = Read-Host "Enter the name the Library (EXAMPLE: 'sitePages)'"
    $AuthorEmail = Read-Host "Enter the name of the Author of the files that you want to copy to the destination site"
    $TargetUrl = Read-Host "Enter the Target Sites Url (EXAMPLE: (No /)sites/365Training/sitePages)"

    Confirmation
    Write-Host "The entered Source site URL is:" $SiteUrl -ForegroundColor Green
    Write-Host "The entered Library Name is:" $LibraryName -ForegroundColor Green
    Write-Host "The entered Author is:" $AuthorEmail -ForegroundColor Green
    Write-Host "The entered Target URL is:" $TargetURL -ForegroundColor Green
    Write-Host "The entered FolderServerRelativePath is:" $FolderServerRelativeUrl/$LibraryName -ForegroundColor Green

    $Confirmation = Read-Host -Prompt "Is this correct? (y/n)"

    }

    while ($Confirmation -ne "y")

    Connect to PnP Online
    Write-Host "Connecting to site" -ForegroundColor Yellow
    Connect-PnPOnline -Url $SiteURL -Interactive
    Write-Host "Connected - Please wait while your files are copied" -ForegroundColor Green

    $allItems = Get-PnPListItem -List "$LibraryName" -FolderServerRelativeUrl "$FolderServerRelativeUrl/$LibraryName" | where {$.FieldValues.Author.Email -eq $AuthorEmail -and $.FileSystemObjectType -eq "File"}

    foreach ($item in $allItems) {
    if ($item.FileSystemObjectType -eq "File") {
    Write-Host "Copying file: $($item.FieldValues.FileLeafRef)" -ForegroundColor green
    Copy-PnPFile -SourceUrl "$($item.FieldValues.FileRef)" -TargetUrl "/$TargetUrl" -Force
    }
    }

    Write-Host "Great, your Files have copied" -ForegroundColor Green

    Pause

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.