Assistance with Sharepoint online script

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 to a different library in another site and on top of that filter the 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! :frowning:

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

Many thanks

Nigel

This is what I have so far:-

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

I have now created a solution that works for me:

My script is as follows:

#Begining 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