Wednesday, February 18, 2026

Create SharePoint Folder Structure in Destination (Only If Not Exists)

Why This Script Is Safe

  • You can run it multiple times

  • It will not create duplicate folders

  • It will only create missing folders

  • Safe for automation

############################################################

# CONFIGURATION

############################################################

 

# Azure AD App Registration details

$ApplicationClientId     = "Client id"

$ApplicationClientSecret = "Client Secret"

$TenantId                = "Tenant ID"

 

# SharePoint Sites

$SourceSiteUrl      = "https://sourcesite.com/sites/site1"

$DestinationSiteUrl = "https://destination.com/sites/site2"

 

############################################################

# SOURCE FOLDER URLS (Full URLs from Source Site)

############################################################

 

$SourceFolderUrls = @(

    "https://sourcesite.com/Shared Documents/folder3/subfolder3.1",

    "https://sourcesite.com/Shared Documents/folder4/subfolder4.1"

)

 

############################################################

# CONNECT TO DESTINATION SITE (Only once)

############################################################

 

Connect-PnPOnline `

    -Url $DestinationSiteUrl `

    -ClientId $ApplicationClientId `

    -ClientSecret $ApplicationClientSecret `

    -Tenant $TenantId `

    -WarningAction Ignore

 

############################################################

# PROCESS EACH FOLDER

############################################################

 

foreach ($FullFolderUrl in $SourceFolderUrls) {

    Write-Host "Processing: $FullFolderUrl" -ForegroundColor Cyan

 

    # Convert URL to URI object

    $Uri = [System.Uri]$FullFolderUrl

 

    # Get server-relative path

    # Example: /Shared Documents/folder3/subfolder3.1

    $ServerRelativePath = $Uri.AbsolutePath

 

    # Remove site name to get site-relative path

    # Example result: Shared Documents/folder3/subfolder3.1

    $SiteRelativePath = $ServerRelativePath.Replace(

        "/" + $Uri.AbsolutePath.Split('/')[1] + "/" + $Uri.AbsolutePath.Split('/')[2] + "/",

        ""

    )

 

    ############################################################

    # IMPORTANT:

    # This command CREATES the folder ONLY IF IT DOES NOT EXIST

    # If folder already exists → nothing happens

    # If folder does not exist → it gets created

############################################################


    Resolve-PnPFolder -SiteRelativePath $SiteRelativePath

    Write-Host "Folder ensured (created if missing): $SiteRelativePath" -ForegroundColor Green

}

 

############################################################

# COMPLETED

############################################################

 

Write-Host "Folder structure creation completed successfully." -ForegroundColor Yellow

 

No comments:

Post a Comment

Featured Post

Create SharePoint Folder Structure in Destination (Only If Not Exists)

Why This Script Is Safe You can run it multiple times It will not create duplicate folders It will only create missing folders S...

Popular posts