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, a distinguished professional, boasts an impressive track record as a Microsoft MVP, having achieved this prestigious recognition for the eighth consecutive year since 2015. With an extensive career spanning over 18 years, Adnan has honed his expertise in various domains, notably excelling in SharePoint, Microsoft 365, Microsoft Teams, the .Net Platform, and Microsoft BI. Presently, he holds the esteemed position of Senior Microsoft Consultant at Olive + Goose. Notably, Adnan served as the MCT Regional Lead for the Pakistan Chapter from 2012 to 2017, showcasing his leadership and commitment to fostering growth within the tech community. His journey in the realm of SharePoint spans 14 years, during which he has undertaken diverse projects involving both intranet and internet solutions for both private and government sectors. His impact has transcended geographical boundaries, leaving a mark on projects in the United States and the Gulf region, often collaborating with Fortune 500 companies. Beyond his roles, Adnan is a dedicated educator, sharing his insights and knowledge as a trainer. He also passionately advocates for technology, frequently engaging with the community through speaking engagements in various forums. His multifaceted contributions exemplify his dedication to the tech field and his role in driving its evolution.

Leave a Reply