Introduction
In this article, you will learn how to retrieve, create, update or delete SharePoint sub sites programmatically using CSOM with PowerShell on SharePoint 2013 / SharePoint online.
Steps Involved
The following prerequisites needs to be executed before going for CRUD operations using CSOM PowerShell on SharePoint sites.
- Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll.
- 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"
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
- Initialize client context object with the site URL.
- $siteURL = ""
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context.
- #Not required for on premise site - Start
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
- #Not required for on premise site - End
- If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.
Retrieve Sub Sites
- To get all the sites from the root site collection or from particular site collection, web object needs to be accessed using OpenWeb method. If it is a root site, then RootWeb method can also be used.
- Get all the sub sites using Webs property.
- Load and execute query using the context to see the final results.
- The following code snippet shows you the flow.
- function GetSites()
- {
- try
- {
- # Relative Site URL of root site
- $rootWeb = $ctx.Site.OpenWeb("/")
- $subWebs = $rootWeb.Webs
- $ctx.Load($subWebs)
- $ctx.ExecuteQuery()
- foreach($subWeb in $subWebs)
- {
- Write - Host "Title : "
- $subWeb.Title
- Write - Host "Template : "
- $subWeb.webTemplate
- Write - Host "Description : "
- $subWeb.Description
- }
- }
- catch
- {
- write - host "$($_.Exception.Message)" - foregroundcolor red
- }
- }
Create Sub Site
The sites can be created under any site collection or sites using web creation object. The necessary parameters for creating site are title, URL, web template, language.

Venkata APosted Feb 12, 2020, 9:18 AM
Hi Natarajan,I am getting error while updating or getting site list.Exception calling "ExecuteQuery" with "0" argument(s): "Value cannot be null.
Humayun Kabir MamunPosted May 22, 2016, 7:39 AM
Nice...
Pradeep SahooPosted May 21, 2016, 6:48 PM
Nice share ....
Vignesh ManiPosted May 21, 2016, 4:48 PM
nice
Debasis SahaPosted May 20, 2016, 10:51 AM
Nice One..
Bhushan GawalePosted May 19, 2016, 2:24 AM
Thanks for sharing!