1

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.

0

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.

https://social.technet.microsoft.com/wiki/contents/articles/39980.sharepoint-online-upload-recursive-files-to-document-library-using-powershell.aspx

SharePoint Online: Files operation with PnP Powershell.

https://malcolm1215.wordpress.com/2017/01/05/pnpfilesoperation/

  • 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 '18 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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