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