Creating a subsite is quite an easy job from a SharePoint site, and if you are going to create a SharePoint site using Powershell then it will be time taking option and require more technical skills.

I would not suggest you to use Powershell script for creating one to two sub sites but if you are doing consultancy and have to create site information architecture which can involve number of sub sites then it would be a time taking job if you are going to create them through SharePoint environment.

Here I am sharing a PowerShell script which can be used for creation of a SharePoint online, and you can use it in scenarios where you have to create an information architecture which involve multiple subsites at different levels. Customize the script as per your requirement and you can create all sites with minimum effort and this script can be used for multiple assignments.

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

function createNewSite($siteUrl)
{
    #*** you can also move below line outside the function to get rid of login again if you need to call the function multiple time. ***
    $Cred= Get-Credential
    
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL) 
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    $ctx.Credentials = $credentials 
    Write-host "Creating new site ..."
 
   $newWeb = New-Object Microsoft.SharePoint.Client.WebCreationInformation
    $newWeb.Title = "Sales - US "
    $newWeb.WebTemplate = "STS#0" #Team Site
    $newWeb.Url = "hr"
    $sWeb = $ctx.Web.Webs.Add($newWeb)
    $ctx.ExecuteQuery()
 
    Write-host "the site is created Successfully"
}

In above script I have created a function createNewSite which take site url as parameter where it will create the site. Below is the function call:

createNewSite https://mstalk.sharepoint.com

Note: You need full control on the site for creation of a new sub site.

Adnan is six time Microsoft MVP (Since 2015) with over 16 years of extensive experience with major expertise on SharePoint, SharePoint based development, Microsoft 365, Microsoft Teams, .Net Platform and Microsoft BI. He is currently working Sr Microsoft Consultant at Olive + Goose. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past 12 years and worked on different intranet/intranet solutions for private & govt. sector majorly in United states and Gulf region and have experience of working with multiple Fortune 500 companies. He is a trainer, technology evangelist and also speaks in community forums.

Leave a Reply