Create Subsite using PowerShell in SharePoint Online

Creating Sub Site in SPO
In my previous articles we learned how to create Site Collection in SharePoint Online now let us learn the next step creating sub sites using PowerShell. It is always a good idea to create sub sites to organize user content for better collaboration across the organization thought it is SharePoint online.

PowerShell Script to create sub site using PowerShell SharePoint Online:

The below script will create a sub site in SharePoint online:




function new-spOnlineWeb {

#variables that needs to be set before starting the script

$siteURL = “<< SharePoint Site Collection URL >>”

$webURL = “MyFirstWeb”

$title = “My First Web”

$template = “STS#0”

$userName = “<< User-ID >>”

$useSamePermissionsAsParentSite = $true

# Let the user fill in their password in the PowerShell window

$password = Read-Host “Please enter the password for $($userName)” -AsSecureString

# set SharePoint Online credentials

$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)

#Creating client context object

$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)

$context.credentials = $SPOCredentials

#create web using WebCreationInformatin object (wci)

$wci = New-Object Microsoft.SharePoint.Client.WebCreationInformation

$wci.url = $webURL

$wci.title = $title

$wci.webtemplate = $template

$wci.useSamePermissionsAsParentSite = $useSamePermissionsAsParentSite

$createWeb = $context.web.webs.add($wci)

$context.load($createWeb)

#send the request containing all operations to the server

try{

$context.executeQuery()

write-host “info: Creating $($title)” -foregroundcolor green

}

catch{

write-host “info: $($_.Exception.Message)” -foregroundcolor red

}

}

new-spOnlineWeb


The below is the expected output:

How To Create A Sub Site In SharePoint Online
How To Create A Sub Site In SharePoint Online
Creating Sub Site in SPO
Creating Sub Site in SPO

Hope this helps, happy development in SharePoint Online !!!


You May Also like the Following SharePoint Online Tutorials:

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →