Forum Discussion
Problems moving files from site to site | PnP Sharepoint
- Nov 01, 2023
In case someone has the same problem as me, this is how i solved the problem.
When connecting I connect to the source site
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Source Site"
then, when you use the move command use it like this:
$SourceFileURL= "/sites/mySite/folder/file.txt" #Site Relative URL from the current site $TargetFileURL ="/sites/anotherSite/folder" Move-PnPFile -SourceUrl $SourceFileURL -TargetUrl $TargetFileURL
if u use /sites/<the name of the site> you will be able to move the files the any site you want
Hi afierro97,
Thanks for your question.
The problem you are facing is that the Copy-PnPFile cmdlet requires the source file to exist on the same site collection as the destination site collection. This is because the cmdlet uses the SharePoint migration infrastructure to copy the file.
You can try to use this script adapting it to your needs:
# Connect to the source site
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Source Site"
# Get the PnPFile object for the file that you want to move
$sourceFile = Get-PnPFile -Url "https://tenant.sharepoint.com/sites/Source Site/Documents/myfile.txt"
# Disconnect from the source site
Disconnect-PnPOnline
# Connect to the destination site
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/destination Site"
# Copy the file from the source site to the destination site
Copy-PnPFile -SourceUrl $sourceFile.ServerRelativeUrl -TargetUrl "https://tenant.sharepoint.com/sites/destination Site/Documents"
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)
In case someone has the same problem as me, this is how i solved the problem.
When connecting I connect to the source site
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Source Site"
then, when you use the move command use it like this:
$SourceFileURL= "/sites/mySite/folder/file.txt" #Site Relative URL from the current site
$TargetFileURL ="/sites/anotherSite/folder"
Move-PnPFile -SourceUrl $SourceFileURL -TargetUrl $TargetFileURL
if u use /sites/<the name of the site> you will be able to move the files the any site you want