Share via

PowerShell for SharePoint

Anonymous
2022-07-29T14:33:15+00:00

Hi all,
I've spent several weeks trying to figure out how to copy content between SharePoint Communications Sites. I have been succesful with pages and images (correction most images), but have failed to copy files and themes. I've been using PowerShell and Power Automate and also attempted using the code shown below that I found on the internet (see sample below). As I am still a relative novice with SharePoint admin and PowerShell, are there certain limitations that I need to know before files can be copied (for instance do both sites need to be made public?)
Any guidance would be appreciated.

Thanks in advance,
David

#Parameters $SiteURL = "https://contoso.sharepoint.com/sites/marketing"``$SourceURL = "/sites/marketing/Shared Documents/Active/SRS.docx"``$TargetURL = "/sites/marketing/Shared Documents/Categorized/SRS.docx"``#Connect to PnP Online``Connect-PnPOnline -Url $SiteURL -Interactive`` ``#Copy All Files and Folders from one folder to another``Copy-PnPFile -SourceUrl $SourceURL -TargetUrl $TargetURL -Force

Microsoft 365 and Office | SharePoint | For education | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2022-08-04T02:45:39+00:00

Dear David,

Thanks for your updates.

I'm afraid that the PnP script may not copy the files if the SourceFolderURL is the root folder of the library.

There may be two workarounds applied to your requirements.

  1. Put all other folders and files in a folder, define the folder as SourceFolderURL and run the script with SharePoint Online PnP.
  2. Use Power Automate to copy all folders, files and subfolders in the parent folders and files between libraries in different sites.

For example, you have the following folders and files in Libvrary809 in SearchTest1. Folder1 contains Subfolder1, Subfolder1 contains Document1.docx and you want to copy Folder1 with Subfolder1 and Document1.docx, Folder2 and Document2.docx to NewLibrary0A in Site0111.

You can create the flow as follows.

Condition:

IsFolder: true

Folder path: Libvrary809/

Condition2:

IsFolder: false

Folder path: Libvrary809/

Result:

Thanks for your understanding and have a nice day!

Sincerely,

Cliff | Microsoft Community Moderator

***Note: In the event that you're unable to reply to this thread, please ensure that your Email address is verified in the Community Website by clicking on Your Account Name > "My Profile" > "Edit Profile" > Add your Email Address > tick "Receive email notifications" checkbox > click on "Save". ***

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2022-08-03T11:49:39+00:00

    Thanks Cliff, that guidance worked. Yay!

    I have one question though (in fact I have several more but, although they are related, I am going to raise them as a separate case so as to keep this query concise.)

    The one question here however, is how do I copy several folders and files in one go?

    I thought that if I used the root folder as the source folder URL, it might do the trick, but it didn't. The following didn't work.

    $SourceFolderURL= "Shared Documents/" 

    $TargetFolderURL = "/sites/TestCommunicationsSite3/Shared Documents" 

    What should be the file path to copy every folder and file?

    Once again thanks for your valuable help,

    David

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-08-02T01:30:21+00:00

    Dear David,

    Thanks for your updates and providing the detailed screenshots for us to clarify the situation in your environment.

    In your script, the source URL and the target URL may be needed to be changed.

    Based on the screenshots, you may change them to the following in your environment.

    $SourceFolderURL= "Shared Documents/Courses and Training" 

    $TargetFolderURL = "/sites/TestCommunicationsSite3/Shared Documents" 

    So the full script may be the following in your environment.

    #Config Variables

    $SiteURL = "https://xxxx.sharepoint.com/sites/TestCommunicationsSite"

    $SourceFolderURL= "Shared Documents/Courses and Training" 

    $TargetFolderURL = "/sites/TestCommunicationsSite3/Shared Documents" 

    #Connect to PnP Online

    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

    #Copy Folder to different site collection

    Copy-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Force

    As the script is using SharePoint Online PnP, I also suggest you check whether you have download SharePoint Online Management Shell from here and installed SharePoint Online PnP.

    Thanks for your effort and time.

    Sincerely,

    Cliff | Microsoft Community Moderator

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-08-01T12:51:34+00:00

    Thanks for your contribution Cliff, but alas it's still not fulfilling the brief.

    The script runs without any error messages, but nothing is copied.

    I'm wondering if I am using the correct folder URLs because they look quite a bit different to yours. Below are the scripts to the 2 tests and the screen shots of the folders and before and after (xxxx replacing my test tennant site):

    Test 1

    $SiteURL = "https://xxxx.sharepoint.com/sites/TestCommunicationsSite" 

    $SourceFolderURL= "https://xxxx.sharepoint.com/sites/TestCommunicationsSite/Shared%20Documents/Forms/AllItems.aspx?id=xxxx" 

    $TargetFolderURL = "https://xxxx.sharepoint.com/sites/TestCommunicationsSite3/Shared%20Documents/Forms/AllItems.aspx" 

    #Connect to PnP Online 

    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential) 

    cmdlet Get-Credential at command pipeline position 1

    Supply values for the following parameters:

    Test 2 - here I deleted the extra site adress script in line with your format

    PS C:\WINDOWS\system32> #Config Variables 

    $SiteURL = "https://xxxx.sharepoint.com/sites/TestCommunicationsSite" 

    $SourceFolderURL= "Shared%20Documents%2FCourses%20and%20Training&viewid=xxxx" 

    $TargetFolderURL = "/sites/TestCommunicationsSite3/Shared%20Documents/Forms/AllItems.aspx" 

    #Connect to PnP Online 

    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential) 

    #Copy Folder to different site collection 

    Copy-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Force

    Disconnect-PnPOnline 

    Here's the source folder:

    Image

    Here's the destination folder before and after the run:

    Image

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2022-07-29T20:35:06+00:00

    Dear David,

    Good day! Thank you for posting in Microsoft Community. We are happy to help you.

    In your script, the source url should be the folder name in the original site and the target url should be the server related url of the target folder or target library as you are using the Copy-PnPFile to copy all files and subfolders in a folder to another folder or another library in a different site.

    For example, I have Folder1 in site community01 and want to copy the Folder1 to the site library Shared Documents in site community02. In this scenario, please use the script below to see the result in SharePoint Online PnP.

    #Config Variables $SiteURL = "https://contoso.sharepoint.com/sites/community01/"

    $SourceFolderURL= "Shared Documents/Folder1"

    $TargetFolderURL = "/sites/community02/Shared Documents"

    #Connect to PnP Online

    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

    #Copy Folder to different site collection

    Copy-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Force

    Sincerely,

    Cliff | Microsoft Community Moderator

    ***Note: In the event that you're unable to reply to this thread, please ensure that your Email address is verified in the Community Website by clicking on Your Account Name > "My Profile" > "Edit Profile" > Add your Email Address > tick "Receive email notifications" checkbox > click on "Save".***

    Was this answer helpful?

    0 comments No comments