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)
- afierro97Nov 01, 2023Copper Contributor
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
- afierro97Oct 19, 2023Copper Contributor
Hi LeonPavesic! Thank you for the answer but that didn't work for me.
After running the command and saving it to a variable It won't save nothing under the "ServerRelativeURL".
Line | 11 | Copy-PnPFile -SourceUrl $sourceFile.ServerRelativeUrl -TargetUrl "ht … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Cannot bind argument to parameter 'SourceUrl' because it is null.
It's weird because if I print only what $sourceFile saved it shows the document I want. I'll investigate further on the problem of using that property
Also I made a mistake posting the question the first time, I posted the copy command and wanted to show a move one but it gave me the exact same error. I've already fix the error in the post.