I am writing a powershell script to copy files (documents) from a OneDrive site to a SharePoint site. They are in different site collections.

I am using this command:

Copy-PnPFile -SourceUrl $file.fieldvalues.FileRef -TargetUrl $destpath -OverwriteIfAlreadyExists -Force -ErrorVariable errors -ErrorAction SilentlyContinue

But it is giving me 404 error. I think the error is coming from the -sourceurl parameter since they are from different site collection. I tried adding the whole url of the source file but it gave me this error:

Copy-PnPFile : Value does not fall within the expected range.

I can just load the files using $docLibrary = $ctx.Web.Lists.GetByTitle("Documents"), but I'm not sure how to copy its contents to the SharePoint site. Is there any possible approach?

P.S: I am getting the files form the onedrive using $items = Get-PnPListItem -List Documents -PageSize 1000.

1 Answer 1


You could save the file in the local folder in computer firstly and then upload the file from the local folder to the document library in SharePoint online.

More references:

SharePoint Online : Upload recursive files to Document library using PowerShell.


SharePoint Online: Files operation with PnP Powershell.


  • Thank you!! i've done exactly this one and its working perf. however, just a quick question. we can copy files from one drive to other site collections manually through the list UI but not sure why there are no powershell command for it. or i am completely missing out on this
    – Page F.P.T
    Jan 4, 2018 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.