In my previous blog we have created a site using UI. In this blog we will go through, how to create a site using PowerShell.
We can create a site in 4 simple steps using PowerShell and CSOM.
Step 1: Add client dll to include CSOM in the script
  1. Add - Type - Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
  2. Add - Type - Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Step 2: Collect site collection URL and Credential of the user
  1. $siteUrl = https: //[yoururl].sharepoint.com/sites/[site collection name ]
  2. $username = "[your]@[id].onmicrosoft.com"
  3. $webTemp = "BLANKINTERNET#0"
Step 3: Get context and provide required information
  1. $context = New - Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
  2. $credentials = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
  3. $context.Credentials = $credentials
  4. $webInfo = New - Object Microsoft.SharePoint.Client.WebCreationInformation
  5. $webInfo.Url = "Educationcenter"
  6. $webInfo.Title = "Education Center"
  7. $webInfo.WebTemplate = $webTemp
  8. $newWeb = $context.Web.Webs.Add($webInfo)
All available WebTemplate information can be found here.
Step 4: create the site
  1. $context.Load($newWeb)
  2. $context.ExecuteQuery()