A parameter cannot be found that matches parameter name 'List'

DKADM 0 Reputation points
2025-01-30T21:38:45.6166667+00:00

I have been trying to build a powershell runbook that captures the source document library's folder structure and cloning it to a target sites document library using PnP PowerShell. I keep running into the same error that I can't seem to resolve. At first I thought is was the space in the source and target library strings 'Shared Library', so I replaced the space with '%20', but the error persists. Does anyone know where I'm going wrong? I can't for life of me see what the issue is.

Any advice would be great

Thank you in advance

D

#Azure Cert
$password = Get-AutomationVariable -Name 'PnP-module-cert-pwd'
$secPassword = $password | ConvertTo-SecureString -AsPlainText -Force
$cert = Get-AutomationCertificate -Name 'SharePoint PnP PowerShell Connection.pfx'
$pfxCert = $cert.Export("pfx", $password)
$certPath = "$env:TEMP\SharePointPnPConnection.pfx"
Set-Content -Value $pfxCert -Path $certPath -Force -AsByteStream

# Import PnP PowerShell Module
Import-Module PnP.PowerShell

# Variables
$sourceSiteUrl = "https://mysite.sharepoint.com/sites/DevelopmentTestSite/"
$targetSiteUrl = "https://mysite.sharepoint.com/sites/DevelopmentTestSiteTARGET/"
$sourceLibraryName = "Shared Documents"
$targetLibraryName = "Shared Documents"

# Connect to the source site Credentail from Cert
Connect-PnPOnline -Url $sourceSiteUrl -ClientId 'b372a83b-2345243p2345245234524-573e73070227' -Tenant '4f5a8277-q234-454-545-454-c5784d0c' -CertificatePath $certPath -CertificatePassword $secPassword
Write-Output "Connected to source site: $sourceSiteUrl"

# Get all folders and subfolders in the source document library
$sourceFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $sourceLibraryName -ItemType Folder

# Connect to the target site Credentail from Cert
Connect-PnPOnline -Url $targetSiteUrl -ClientId 'b372a83b-2345243p2345245234524-573e73070227' -Tenant '4f5a8277-q234-454-545-454-c5784d0c' -CertificatePath $certPath -CertificatePassword $secPassword
Write-Output "Connected to source site: $targetSiteUrl"


# For each folder in the source library, create the same folder structure in the target library
foreach ($folder in $sourceFolders) {
    # Skip the root folder (it already exists)
    if ($folder.ServerRelativeUrl -eq "/sites/$sourceSiteUrl/$sourceLibraryName") {
        continue
    }

    # Get the relative path of the folder
    $relativeFolderPath = $folder.ServerRelativeUrl.Replace("/sites/$sourceSiteUrl/$sourceLibraryName", "")

    # Construct the target folder path in the target library
    $targetFolderPath = "/sites/$targetSiteUrl/$targetLibraryName$relativeFolderPath"

    # Create the folder in the target document library
    Add-PnPFolder -Name $folder.Name -Folder $targetFolderPath -List $targetLibraryName 
            Write-Host "Created folder: $targetFolderPath"
}

# Disconnect from SharePoint
Disconnect-PnPOnline

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,300 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Vinod Pittala 235 Reputation points Microsoft Vendor
    2025-01-31T00:30:39.1633333+00:00

    Hello DKADM,
    Welcome to Microsoft Q&A Forum, thank you for posting your query here!

    We will try to validate the entire script once, however, please replace the ClientId and TenantId with <b372a83b-2345243p2345245234524-573e73070227> format, instead of 'b372a83b-2345243p2345245234524-573e73070227 '. or else you can add it over the variables as like:

    $clinetId = b372a83b-2345243p2345245234524-573e73070227

    $TenantId = 4f5a8277-q234-454-545-454-c5784d0c

    Please refer this document it has been detailed clearly

    let us know if any help, we will always help as you needed.!

    Please do not forget to "Accept the answer” and upvote it wherever the information provided helps you, this can be beneficial to other community members.it would be greatly appreciated and helpful to others.


  2. Madugula Jahnavi 0 Reputation points Microsoft Vendor
    2025-02-06T07:57:49.2566667+00:00

    Hello DKADM,

    Thank you for reaching out to Microsoft Q&A.

    I have made few modifications to your script according to the requirement and the detailed script is given below.

    $password = Get-AutomationVariable -Name 'xxx'
    $secPassword = $password | ConvertTo-SecureString -AsPlainText -Force
    $cert = Get-AutomationCertificate -Name 'xxx.pfx'
    $pfxCert = $cert.Export("pfx", $password)
    $certPath = "certpath"
    Set-Content -Value $pfxCert -Path $certPath -Force -AsByteStream
    Import-Module PnP.PowerShell
    # Variables
    $sourceSiteUrl = "https://newsite.sharepoint.com/sites/DevelopmentTestSite"
    $targetSiteUrl = "https://newsite.sharepoint.com/sites/DevelopmentTestSiteTARGET"
    $sourceLibraryName = "Shared Documents"
    $targetLibraryName = "Shared Documents"
    Connect-PnPOnline -Url $sourceSiteUrl -ClientId 'xxxx' -Tenant 'xxxx' -CertificatePath $certPath -CertificatePassword $secPassword
    Write-Output "Connected to source site: $sourceSiteUrl"
    # Get all folders and subfolders in the source document library
    $sourceFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $sourceLibraryName -ItemType Folder
    # Connect to the target site
    Connect-PnPOnline -Url $targetSiteUrl -ClientId 'xxxx' -Tenant 'xxxx' -CertificatePath $certPath -CertificatePassword $secPassword
    Write-Output "Connected to target site: $targetSiteUrl"
    # Loop through each folder and replicate the structure
    foreach ($folder in $sourceFolders) {
        # Skip the root folder
        if ($folder.ServerRelativeUrl -eq "/sites/DevelopmentTestSite/$sourceLibraryName") {
            continue
        }
        $relativeFolderPath = $folder.ServerRelativeUrl.Replace("/sites/DevelopmentTestSite/$sourceLibraryName", "")
        $parentPath = if ($relativeFolderPath -ne "") { "$targetLibraryName$relativeFolderPath" } 
    	else { $targetLibraryName }
        # Create folder in target library
        Add-PnPFolder -Name $folder.Name -Folder $parentPath -List $targetLibraryName
        Write-Host "$parentPath/$folder.Name created"
    }
    Disconnect-PnPOnline
    

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.