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
- Add - Type - Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
- 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
- $siteUrl = https:
- $username = "[your]@[id].onmicrosoft.com"
- $webTemp = "BLANKINTERNET#0"
Step 3: Get context and provide required information
- $context = New - Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
- $credentials = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
- $context.Credentials = $credentials
- $webInfo = New - Object Microsoft.SharePoint.Client.WebCreationInformation
- $webInfo.Url = "Educationcenter"
- $webInfo.Title = "Education Center"
- $webInfo.WebTemplate = $webTemp
- $newWeb = $context.Web.Webs.Add($webInfo)
All available WebTemplate information can be found
here.
Step 4: create the site
- $context.Load($newWeb)
- $context.ExecuteQuery()