Thursday, April 25, 2024
HomeMicrosoft 365Effortlessly Create Multiple SharePoint Sites with PowerShell and PnP Framework

Effortlessly Create Multiple SharePoint Sites with PowerShell and PnP Framework

Effortlessly Create Multiple SharePoint Sites with PowerShell and PnP Framework

Introduction

Creating SharePoint sites manually can be a tedious task, especially when you need to create multiple sites at once. However, with a simple PowerShell script using the PnP Framework, you can easily create multiple sites with additional settings.

To begin, ensure that you have the latest version of the PnP Framework PowerShell installed. If not, check out this link to learn how to install the new version:

Installing PnP PowerShell | PnP PowerShell

Next, create a CSV file named “sites.csv” with the following structure:

SiteName, CustomScriptsEnabled Marketing, 1

You can add additional columns for other settings that you want to enable or disable on the site.

Save the “sites.csv” file in the same location as the PowerShell script. The following code snippet shows an example of creating a communication site using the PnP Framework:

# Import PnP PowerShell module
Import-Module SharePointPnPPowerShellOnline -Verbose

# Define site creation function
Function Create-Site([string]$SiteName, [bool]$CustomScriptsEnabled) {
    Write-Host "Creating site $SiteName"
    
    # Uncomment the line below and comment the other to create a modern team site
    #New-PnPTenantSite -Title $SiteName -Url $SiteName -Template "STS#3"
    
    # Create communication site
    New-PnPTenantSite -Title $SiteName -Url $SiteName -Template "SITEPAGEPUBLISHING#0" -Lcid 1033 -AllowFileSharingForGuestUsers $false -TimeZone 10 -SuppressModernAuthForGuestUsers $false -SharingCapability Disabled -DisableFlows $false -CommentsOnSitePagesDisabled $false -MembersCanShare $false -OwnersCanShare $false -Classification "Confidential" -AllowDownload $false -DenyAddAndCustomizePages $CustomScriptsEnabled
}

# Get sites from CSV file
$sites = Import-Csv "sites.csv"

# Loop through sites and create them
ForEach ($site in $sites) {
    Create-Site -SiteName $site.SiteName -CustomScriptsEnabled [bool]$site.CustomScriptsEnabled
}

Most Popular